当我使用gevent
时,我无法使用requests
,我的用法是错误的吗?
from gevent import monkey; monkey.patch_all()
import gevent, requests
requests.get('https://haofly.net')
它引发了错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.3/site-packages/requests/api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "/usr/local/lib/python3.3/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python3.3/site-packages/requests/sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.3/site-packages/requests/sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.3/site-packages/requests/adapters.py", line 423, in send
timeout=timeout
File "/usr/local/lib/python3.3/site-packages/requests/packages/urllib3/connectionpool.py", line 595, in urlopen
chunked=chunked)
File "/usr/local/lib/python3.3/site-packages/requests/packages/urllib3/connectionpool.py", line 352, in _make_request
self._validate_conn(conn)
File "/usr/local/lib/python3.3/site-packages/requests/packages/urllib3/connectionpool.py", line 831, in _validate_conn
conn.connect()
File "/usr/local/lib/python3.3/site-packages/requests/packages/urllib3/connection.py", line 289, in connect
ssl_version=resolved_ssl_version)
File "/usr/local/lib/python3.3/site-packages/requests/packages/urllib3/util/ssl_.py", line 308, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "/usr/local/lib/python3.3/site-packages/gevent/_ssl3.py", line 60, in wrap_socket
_context=self)
File "/usr/local/lib/python3.3/site-packages/gevent/_ssl3.py", line 143, in __init__
if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
NameError: global name 'SOL_SOCKET' is not defined
我的Python版本是3.3
如果我将ssl=False
添加到monkey.patch_all()
,则会返回:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.3/site-packages/requests/api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "/usr/local/lib/python3.3/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python3.3/site-packages/requests/sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.3/site-packages/requests/sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.3/site-packages/requests/adapters.py", line 423, in send
timeout=timeout
File "/usr/local/lib/python3.3/site-packages/requests/packages/urllib3/connectionpool.py", line 595, in urlopen
chunked=chunked)
File "/usr/local/lib/python3.3/site-packages/requests/packages/urllib3/connectionpool.py", line 352, in _make_request
self._validate_conn(conn)
File "/usr/local/lib/python3.3/site-packages/requests/packages/urllib3/connectionpool.py", line 831, in _validate_conn
conn.connect()
File "/usr/local/lib/python3.3/site-packages/requests/packages/urllib3/connection.py", line 289, in connect
ssl_version=resolved_ssl_version)
File "/usr/local/lib/python3.3/site-packages/requests/packages/urllib3/util/ssl_.py", line 308, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "/usr/local/lib/python3.3/ssl.py", line 245, in wrap_socket
_context=self)
File "/usr/local/lib/python3.3/ssl.py", line 335, in __init__
server_hostname)
TypeError: _wrap_socket() argument 1 must be _socket.socket, not SSLSocket
答案 0 :(得分:2)
这是gevent中的一个错误。它正在尝试从ssl
库中导入socket
constant。
将特定常量发生导入ssl
库,因为该库使用它。但它只是因为issue 19422的修复程序需要将其导入ssl
模块。该错误的修复程序包含在Python 3.3.4中,并且您具有版本3.3.3,因此您在ssl
模块中也没有可用的特定常量。
您可以通过在from socket import SOL_SOCKET, SO_TYPE
(位于系统的gevent/_ssl3.py
目录中)添加/usr/local/lib/python3.3/site-packages
来解决此问题。或者您可以升级到Python 3.3.4或更高版本。
我已将issue #856与gevent
项目一起提交。
答案 1 :(得分:0)
安装Python 3.5版本可以解决这个问题。