模块 chembl_webresource_client.new_client 失败,因为 python 2.7.9 下的 urllib3 中的 ssl 损坏

Module chembl_webresource_client.new_client fails because broken ssl in urllib3 under python 2.7.9

模块 chembl_webresource_client.new_client(版本 0.8.5)可从 pypi 获得,是 python 2.7.9 中的 client for accessing the new web service API of the ChEMBL database. It uses urllib3 which is broken for a while,因为缺少 sslwrap内置 ssl 模块。如果我尝试导入模块:

from chembl_webresource_client.new_client import new_client

堆栈跟踪如下所示:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/chembl_webresource_client/new_client.py", line 63, in <module>
    new_client = client_from_url(Settings.Instance().NEW_CLIENT_URL + '/spore')
  File "/usr/lib/python2.7/site-packages/chembl_webresource_client/new_client.py", line 27, in client_from_url
    res = requests.get(url)
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 65, in get
    return request('get', url, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 49, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 461, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 370, in send
    timeout=timeout
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
    body=body, headers=headers)
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 341, in _make_request
    self._validate_conn(conn)
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 762, in _validate_conn
    conn.connect()
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/connection.py", line 238, in connect
    ssl_version=resolved_ssl_version)
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py", line 256, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/usr/lib/python2.7/ssl.py", line 350, in wrap_socket
    _context=self)
TypeError: __init__() got an unexpected keyword argument 'server_hostname'

终于解决了这个问题,我将post下面的解决方案。

首先,我尝试使用 this thread, without success. Finally from this answer 的补丁,我发现安装 pyopensslndg-httpsclientpyasn1 是解决问题所必需的。

pip2 install pyopenssl
pip2 install ndg-httpsclient
pip2 install pyasn1

并且可以导入ChEMBL模块:

from chembl_webresource_client.new_client import new_client