【问题标题】:Certificates repository in PythonPython 中的证书存储库
【发布时间】:2015-03-18 01:17:14
【问题描述】:

我在 python 中使用请求,我想使用 SSL。

>>> requests.get('https://github.com', verify=True)
<Response [200]>

文档说:

您可以使用受信任 CA 的证书验证 CA_BUNDLE 文件的路径。这个受信任的 CA 列表也可以通过 REQUESTS_CA_BUNDLE 环境变量来指定。

有人知道如何配置此环境变量或信任证书吗?

谢谢!!

【问题讨论】:

标签: python ssl certificate python-requests


【解决方案1】:

查看 python-requests 代码:

            # Look for requests environment configuration and be compatible
            # with cURL.
            if verify is True or verify is None:
                verify = (os.environ.get('REQUESTS_CA_BUNDLE') or
                          os.environ.get('CURL_CA_BUNDLE'))

因此您必须设置其中任何一个环境变量才能进行 SSL 调用。

设置环境变量REQUESTS_CA_BUNDLE:

$ export REQUESTS_CA_BUNDLE=/etc/ssl/certs/foo.crt

或将其设置为目录

$ export REQUESTS_CA_BUNDLE=/etc/ssl/certs

http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification

Note:
If verify is set to a path to a directory, the directory must have been
processed using the c_rehash utility supplied with OpenSSL.

所以你必须重新哈希目录中的那些证书:

$ cd /etc/ssl/certs
$ for i in *.crt; do ln -s $i $(openssl x509 -hash -noout -in $i).0; done

$ c_rehash /etc/ssl/certs

【讨论】:

  • 对于想知道如何将多个 .pem 证书添加到您的 CA_BUNDLE 文件中的任何人,可以将它们彼此附加到同一个文件中。运行python -c "import requests; print(requests.certs.where())",并打开打印的文件路径以查看示例。
【解决方案2】:

verify 设置为 True 应该已经使用 SSL:

http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification

或者,您有本地证书吗? (如果这是您想要的,请将 verify 设置为 cert.pem 文件的绝对路径。)

【讨论】:

  • 我有一个来自服务器的本地证书(我已经从 firefox 导出了证书)是一个自签名证书。我使用了 cert.pem 文件的路径,但给了我一个例外。 requests.exceptions.SSLError: [SSL] PEM lib <2525>
猜你喜欢
  • 1970-01-01
  • 2013-03-30
  • 1970-01-01
  • 1970-01-01
  • 2022-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-09
相关资源
最近更新 更多