python3 全局名称'SOL_SOCKET 未定义

python3 global name 'SOL_SOCKET is not defined

我用了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(),它 returns:

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

安装Python3.5版本可以解决这个问题。

这确实是gevent的一个bug。它正在尝试从 ssl 库中导入 socket constant

那个特定常量 碰巧 被导入到 ssl 库中,因为该库使用它。但它之所以存在,是因为 issue 19422 的修复要求将其导入到 ssl 模块中。该错误的修复包含在 Python 3.3.4 中,而您的版本是 3.3.3,因此您在 ssl 模块中也没有可用的特定常量。

您可以通过在 gevent/_ssl3.py(位于您系统的 /usr/local/lib/python3.3/site-packages 目录中)中添加 from socket import SOL_SOCKET, SO_TYPE 来修复它。或者您可以升级到 Python 3.3.4 或更高版本。

我已将其作为 issue #856 提交给 gevent 项目。