Make requests using Python over Tor
Here is the code you want to use (download the stem package using pip install stem
)
from stem import Signal
from stem.control import Controller
with Controller.from_port(port = 9051) as controller:
controller.authenticate(password='your password set for tor controller port in torrc')
print("Success!")
controller.signal(Signal.NEWNYM)
print("New Tor connection processed")
Good luck and hopefully that works.
Urllib2 using Tor and socks in python
To get ip from specific country you have to set two parameters ExitNodes={countrycode} and StrictNodes=1.Here for India country code is {in}.To know country code check http://www.b3rn3d.com/blog/2014/03/05/tor-country-codes/.
Using python you can set the these parameters as follows
Code:-
import socks
import socket
import urllib2
def newIdentity(self):
socks.setdefaultproxy()
s= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("127.0.0.1", 9051))
s.send('AUTHENTICATE "my_password" \r\n')
response = s.recv(128)
if response.startswith("250"):
s.send("SETCONF ExitNodes={in}\r\n")
s.send("SETCONF StrictNodes=1\r\n")
s.send("SIGNAL NEWNYM\r\n")
s.close()
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket
if __name__ == '__main__':
newIdentity()
print urllib2.urlopen('http://my-ip.herokuapp.com').read()
how to access a website with python through tor, successfully? ports 8118 and 9050 don't work?
TOR provides a SOCKS proxy. Since urllib2 can only handle HTTP proxies, you'll have to use a SOCKS implementation.
Tor doesn't work with urllib2
Maybe this answer is related.
In a nutshell, you're using 127.0.0.1:8118
as a proxy url, and http as your protocol, but the protocol should be https
instead.
Related Topics
Pandas Fill Missing Values in Dataframe from Another Dataframe
Boto3 to Download All Files from a S3 Bucket
Differences Between Numpy.Random and Random.Random in Python
Python Pip Specify a Library Directory and an Include Directory
Easy Pretty Printing of Floats
Implement Matlab's Im2Col 'Sliding' in Python
Differencebetween Pylab and Pyplot
The Problem with Installing Pil Using Virtualenv or Buildout
Python Pandas Extract Year from Datetime: Df['Year'] = Df['Date'].Year Is Not Working
Python-Requests Close Http Connection
Strange Behavior of Lists in Python
Check List of Words in Another String
How to Open a File for Exclusive Access in Python
Why Don't Methods Have Reference Equality
What's the Difference Between _Builtin_ and _Builtins_
Most Efficient Property to Hash for Numpy Array