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

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


该文件说:


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


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

谢谢!!

最佳答案

研究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/

10-09 06:47
查看更多