Apparently, there isn't much information regarding how to request a new identity or how to use the ToR proxy in Python. So here's a gift from me to the world.
First, while not necessary, I always use a new ToR identity prior to download something through the ToR network. The code looks like:
import socketdef renewTorIdentity():success = Falses = socket.socket()s.connect(('localhost', 9051))s.send("AUTHENTICATE\r\n")resp = s.recv(1024)if resp.startswith('250'):s.send("signal NEWNYM\r\n")resp2 = s.recv(1024)if resp.startswith('250'):success = Truereturn success
That would work assuming "authentication" is set to none in the Vidalia control panel.
After having a new identity, downloading a file is really simple, as Vidalia comes bundled with privoxy configured on port 8118. Here's a sample code for downloading something:
import urllib2url = "http://something-to-download"proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})opener = urllib2.build_opener(proxy_support)opener.addheaders = [('User-agent', 'Mozilla/5.0')]response = opener.open(url).read()
That's it. Simple.
Hi, do you mean resp2 in the second if statement?
ReplyDeleteThank you
Nick - yes, I did. Thanks.
ReplyDeleteHi, I edited your script and explained how to add authentication
ReplyDeletepingback
http://keymuck.blogspot.com/2012/02/18-python-renew-tor-identity.html
Hi! I can't seem to get the download working. I guess the "Bandwidth Graph" should show something when downloading occurs, right?
ReplyDeleteThis works perfectly, and I can use it no problem. But I'm really interested in knowing why it works, could you point me in some way to read more on the topic and actually understand what I'm using?
ReplyDeleteThanks for the code!