在Python请求中信任自签名证书
我的 Apache SSL 配置有以下设置:
# Server Certificate:
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
# Server Private Key:
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
我没有这个服务器的 CA 证书。我还能把 localhost.crt 安装到我的客户端上,以成功验证我的服务器吗?
在客户端: 我使用的是 Python 的 requests 库(版本 2.2.1)。我使用的是默认的 CA BUNDLE 路径。即使我把 localhost.crt 添加到默认路径下的 cacert.pem 中,我仍然无法通过验证。我看到的异常是:
File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 385, in send
raise SSLError(e)
SSLError: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
我是不是做错了什么?我是不是只需要添加签署 localhost.crt 的 CA 证书到服务器上?
谢谢, Vijay
1 个回答
5
如果你能提供一些代码,并且更清楚地说明你在做什么,那么你会得到一个好的答案。
如果你不想因为使用无效证书而出现错误,可以试试 verify=False
这个选项。
>>> requests.get('https://kennethreitz.com', verify=False)
如果你想使用自定义证书,可以把证书放在脚本的文件夹里,然后使用 cert=('/path/client.cert', '/path/client.key')
这个参数。
>>> requests.get('https://kennethreitz.com', cert=('/path/client.cert', '/path/client.key'))
.
想了解更多信息,可以查看 docs.python-requests.org/en/master/user/advanced/ 网站。