我复制粘贴了另一篇文章的代码,对于他们来说,似乎没问题,但是我打印错误,我不知道为什么...... How to make urllib2 requests through Tor in Python? 有人可以帮我解决这个错误吗?
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9150)
socket.socket = socks.socksocket
import urllib.request
print(urllib.request.urlopen('http://www.google.com').read())
Traceback (most recent call last):
File "/Users/Raphael/Desktop/whatsmyip.py", line 17, in <module>
r = requests.get("http://checkip.dyn.com")
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/api.py", line 68, in get
return request('get', url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/adapters.py", line 370, in send
timeout=timeout
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 349, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1088, in request
self._send_request(method, url, body, headers)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1126, in _send_request
self.endheaders(body)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1084, in endheaders
self._send_output(message_body)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 922, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 857, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 155, in connect
conn = self._new_conn()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 134, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 78, in create_connection
sock.connect(sa)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socks.py", line 369, in connect
self.__negotiatesocks5(destpair[0],destpair[1])
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socks.py", line 176, in __negotiatesocks5
self.sendall("\x05\x01\x00")
TypeError: 'str' does not support the buffer interface
有谁知道为什么?我真的很想使用这个模块,但它不起作用!