3
votes

I am pretty new to python. I am trying to write a pretty simple web scraper for a project I am working on. In the process I am trying to use Tor to change my IP address so I don't get disconnected from the service I am scraping. I was trying to test the code specific to getting a new IP before adding it to my project. Here is the code I am testing.

from TorCtl import TorCtl
import urllib2

for i in range(1,51):
    proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"} )
    opener = urllib2.build_opener(proxy_support)
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    urllib2.install_opener(opener)

    print "IP " + str(i) + ":"
    print urllib2.urlopen('http://ifconfig.me/ip').read()

    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="torPass")
    conn.sendAndRecv('signal newnymrn')
    conn.close()

When I do this i get the following error:

IP 1: Traceback (most recent call last): File "scrapingTools.py", line 86, in main() File "scrapingTools.py", line 76, in main print urllib2.urlopen('http://ifconfig.me/ip').read() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 394, in open response = self._open(req, data) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 412, in _open '_open', req) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain result = func(*args) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1199, in http_open return self.do_open(httplib.HTTPConnection, req) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1174, in do_open raise URLError(err) urllib2.URLError:

Any help understanding what is going on here would be greatly appreciated.

2
+1 for ifconfig.me/ip, I like that.pguardiario

2 Answers

0
votes

There is some problem with your proxy configuration. Your code works without the proxy settings.

0
votes

I don't know anything about TorCtl but you're not sending an AUTHENTICATE string, tor will expect that. It should look something like:

telnet localhost:9051
>> 250 OK
AUTHENTICATE "xxx"
>> 250 OK
signal NEWNYM
>> 250 OK

Note, wait a few seconds for the identity to have changed.