python - 使用虚拟环境时的证书验证

标签 python python-requests virtualenv

我的机器上安装了根 CA 证书,在使用请求库的系统安装时发出请求时一切正常:

$ python -c 'import requests; print requests.get("https://example.com")'
<Response [200]>

但是,如果我从虚拟环境中发出相同的请求,证书验证会失败:

$ python -c 'import requests; print requests.get("https://example.com")'
requests.exceptions.SSLError: [Errno 1] _ssl.c:510: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

使用 requests.certs.where 我可以看到系统安装使用系统 CA 包,虚拟环境使用随请求一起提供的 CA 包:

$ python -c "import requests; print requests.certs.where()"
/etc/ssl/certs/ca-certificates.crt

$ (venv) python -c "import requests; print requests.certs.where()"                                                            
.../venv/local/lib/python2.7/site-packages/requests/cacert.pem

在使用 virtualenv 时,是否有另一种解决方案可以在不为每个请求提供路径的情况下获取系统证书,即:

>>> requests.get("https://example.com" verify="/etc/ssl/certs/ca-certificates.crt")

最佳答案

如果你想插入一个特定的 CA 证书到你的 virtualenv 的 CA 包,你可以附加到它:

openssl x509 -in $specific_ca.crt -text >> $virtualenv/lib/python2.7/site-packages/certifi/cacert.pem

关于python - 使用虚拟环境时的证书验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34931378/

相关文章:

html - Python3.5 BeautifulSoup4 从div中的 'p'获取文本

python - 是否有一个 Python HTTP 客户端可以让您在 Content-Disposition 中设置大小以发布文件?

python - 在现有目录中创建 virtualenv 而不创建 "local"目录

python - 为什么在 virtualenv 中运行 mysqld 时会出错?

python - OS X 上的 Virtualenv 安装

python - matplotlib 设置共享轴

python - 尝试在 Google Cloud Storage 上上传 pdf 文件时出现 HttpError 400

python - ProactorEventLoop - 值错误 : loop argument must agree with Future

Python:base64.b64decode() 与 .decode?

Python 请求带有 Unicode 参数的 URL