Bun*_*nyk 6 python ssl httplib2
我正在尝试使用带有httplib2的Github API.但是当我向它的端点发出请求时,它会给我以下错误:
import httplib2
h = httplib2.Http()
h.request('https://api.github.com/gists')
# OUT: Traceback (most recent call last):
# OUT: File "<input>", line 1, in <module>
# OUT: File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1570, in request
# OUT: (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
# OUT: File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1317, in _request
# OUT: (response, content) = self._conn_request(conn, request_uri, method, body, headers)
# OUT: File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1252, in _conn_request
# OUT: conn.connect()
# OUT: File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1044, in connect
# OUT: raise SSLHandshakeError(e)
# OUT: SSLHandshakeError: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Run Code Online (Sandbox Code Playgroud)
我可以使用以下解决方法:
h = httplib2.Http(disable_ssl_certificate_validation=True)
h.request('https://api.github.com/gists')
# OUT: ({'content-length': '58443' ...
Run Code Online (Sandbox Code Playgroud)
但这仍然是一种解决方法,我想知道如何使用httplib2正确验证Github的SSL证书.搜索谷歌我发现我应该更新cacerts.txt该库,但不知道如何以及获取Github的证书权限.或者有没有其他正确的方式通过https发送请求,没有证书验证问题?
UPD:最简单的方法是在 Firefox 中打开 GitHub,查看页面信息 -> 安全 -> 查看证书 -> 详细信息 -> 导出 -> 作为 PEM 文件。而且最好使用请求。
从Firefox提供的有关https连接的信息中,我发现GitHub的证书是“DigiCert High Assurance EV Root CA”,可以在这里找到: http: //curl.haxx.se/ca/cacert.pem
证书文本可以粘贴到httplib2.__path__ + '/cacerts.txt',或保存到单独的文件,并且应使用以下命令创建 http 连接:
h = httplib2.Http(ca_certs='/path/to/that/file')
Run Code Online (Sandbox Code Playgroud)