我正在尝试通过TOR收听GET请求,该请求正在收听127.0.0.1:9050
我已经安装了请求socks:pip install -U requests [socks]
import requests
tor_proxy = {'http': 'socks5://127.0.0.1:9050', 'https': 'socks5://127.0.0.1:9050'}
r = requests.get("http://www.google.com", proxies=tor_proxy, timeout=20)
这是我运行代码时的错误
Traceback (most recent call last):
r = requests.get("http://www.google.com", proxies=tor_proxy, timeout=20)
File "C:\Python27\lib\site-packages\requests\api.py", line 71, in get
return request('get', url, params=params, **kwargs)
File "C:\Python27\lib\site-packages\requests\api.py", line 57, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 585, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 403, in send
timeout=timeout
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 578, in urlopen
chunked=chunked)
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 362, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Python27\lib\httplib.py", line 1057, in request
self._send_request(method, url, body, headers)
File "C:\Python27\lib\httplib.py", line 1097, in _send_request
self.endheaders(body)
File "C:\Python27\lib\httplib.py", line 1053, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 897, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 859, in send
self.connect()
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connection.py", line 167, in connect
conn = self._new_conn()
File "C:\Python27\lib\site-packages\requests\packages\urllib3\contrib\socks.py", line 81, in _new_conn
**extra_kw
File "C:\Python27\lib\site-packages\socks.py", line 195, in create_connection
sock.connect((remote_host, remote_port))
File "C:\Python27\lib\site-packages\socks.py", line 747, in connect
negotiate(self, dest_addr, dest_port)
File "C:\Python27\lib\site-packages\socks.py", line 419, in _negotiate_SOCKS5
CONNECT, dest_addr)
File "C:\Python27\lib\site-packages\socks.py", line 482, in _SOCKS5_request
resolved = self._write_SOCKS5_address(dst, writer)
File "C:\Python27\lib\site-packages\socks.py", line 517, in _write_SOCKS5_address
addr_bytes = socket.inet_pton(family, host)
AttributeError: 'module' object has no attribute 'inet_pton'
Jun 11 13:13:55.000 [notice]尝试120秒连接到[scrubbed]:0。放弃。 (等待袜子信息)
由于
答案 0 :(得分:4)
问题在于库本身,特别是PySocks socks.py,inet_pton仅在unix上可用:
socket.inet_pton(address_family,ip_string)
将IP地址从其特定于系列的字符串格式转换为打包的二进制格式。当库或网络协议调用struct in_addr(类似于inet_aton())或struct in6_addr的对象时,inet_pton()非常有用。
address_family支持的值目前是AF_INET和AF_INET6。如果IP地址字符串ip_string无效,将引发socket.error。请注意,确切的有效内容取决于address_family的值和inet_pton()的底层实现。
可用性:Unix(可能不是所有平台)。
一种解决方法是安装win_inet_pton并将其导入脚本中,该方法将自动添加到套接字库中。
答案 1 :(得分:0)
inet_pton()方法是在Python 2.3中引入的,支持大多数类Unix系统。带有Windows支持的inet_pton()是在Python 3.4版Low-level networking interface中引入的。如果你使用Windows,你有两个选择,一个是@Padraic Cunningham说要安装win_inet_pton运行:
来自Native inet_pton and inet_ntop implementation for Python on Windows的pip install win_inet_pton
继续使用Python版本< = 3.3。然后你可以像这样调用库:
import win_inet_pton
import socket
socket.inet_pton(...)
socket.inet_ntop(...)
第二个选项是更新为使用python> = 3.4。
的版本