【发布时间】:2017-02-02 00:55:13
【问题描述】:
我想使用 python 中的 tor 来自动化请求。 我用一个页面做了一个测试来检查IP,它可以工作。
然后我指向了我想要的站点,显然他们避免了 tor 端点,因为(请参阅下面的堆栈跟踪) - 但它可以在 tor 浏览器中使用。
有更好的方法来调试浏览器的响应吗? (例如连接被拒绝)
从python而不是浏览器查询我缺少哪些东西?
我正在尝试类似的东西:
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5 , "127.0.0.1", 9150, True)
socket.socket = socks.socksocket
socks.wrapmodule(requests)
url = "myexample.com"
# r = requests.Session()
s = requests.get(url)
print s
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 404, in open
response = self._open(req, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 422, in _open
'_open', req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1222, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1181, in do_open
h.request(req.get_method(), req.get_selector(), req.data, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 973, in request
self._send_request(method, url, body, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1007, in _send_request
self.endheaders(body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 969, in endheaders
self._send_output(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 829, in _send_output
self.send(msg)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 791, in send
self.connect()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1172, in connect
self.timeout, self.source_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 562, in create_connection
sock.connect(sa)
File "/Library/Python/2.7/site-packages/socks.py", line 747, in connect
negotiate(self, dest_addr, dest_port)
File "/Library/Python/2.7/site-packages/socks.py", line 419, in _negotiate_SOCKS5
CONNECT, dest_addr)
File "/Library/Python/2.7/site-packages/socks.py", line 494, in _SOCKS5_request
raise SOCKS5Error("{0:#04x}: {1}".format(status, error))
socks.SOCKS5Error: 0x01: General SOCKS server failure
【问题讨论】:
-
该代码存在一些问题,但您知道您使用的是哪个版本的请求吗?如果您有 2.10.0 或更高版本,请不要尝试修改可能不起作用的补丁请求,而是使用 proxies
-
谢谢@drew010,它成功了,我还得升级到python3。