我正在尝试将 Tor 与 urllib2 和 polipo 一起使用。我需要的是一种在程序运行时切换到特定出口节点的方法。我在 /etc/tor/torrc 中设置了“AllowDotExit 1”,并尝试了以下方法:
import urllib2
proxy = '127.0.0.1'
port = '8118'
url='http://ifconfig.me.651d7ace80e0b53e6c05eb4db2491264f049df66.exit'
proxyurl = '%s:%s' % (proxy, port)
proxyhandler = urllib2.ProxyHandler({'http': proxyurl})
opener = urllib2.build_opener(proxyhandler)
page = opener.open(url)
print 'Page opened.'
print page.read()
但我得到的是:
:!/usr/bin/env python tortest.py
Traceback (most recent call last):
File "tortest.py", line 18, in <module>
page = opener.open(url, timeout=20)
File "/usr/lib/python2.7/urllib2.py", line 406, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 519, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 444, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 504: Connect to ifconfig.me.651d7ace80e0b53e6c05eb4db2491264f049df66.exit:80 failed: General SOCKS server failure
有人可以帮我吗?