python - Python 中的证书存储库

标签 python ssl certificate python-requests

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

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

文档说:

You can pass verify the path to a CA_BUNDLE file with certificates of trusted CAs. This list of trusted CAs can also be specified through the REQUESTS_CA_BUNDLE environment variable.

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

谢谢!!

最佳答案

查看 python 请求代码:

            # 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

关于python - Python 中的证书存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28034711/

相关文章:

ssl - 从 Symantec 代码签名证书创建 pfx 文件

java - 使用给定的 p12 证书连接到 https 站点

wcf - 无法让 Azure WCF 服务与客户端证书一起使用

python - 如何在 Python2 中使用 "cryptography"库从证书打印公钥?

linux - 如何从源代码升级 CentOS 6.5/Linux/Unix 中的 OpenSSL?

Python 或 IronPython

python - 我们如何将 C 结构传递给 Python?

python - 查找列表是否包含特定的 numpy 数组

PHP7 cURL 和 SOAP 请求 SSL 失败

python - 简单的 Qt 小部件来绘制线条和形状(如 tkinter Canvas )?