简体   繁体   English

Python:urllib3 无法识别系统 CA 证书

[英]Python: System CA certificates not recognized by urllib3

When I try to access any HTTP website, even one of the most popular, I get a SSL warning from urllib3 module.当我尝试访问任何 HTTP 网站时,即使是最受欢迎的网站之一,我也会收到来自 urllib3 模块的 SSL 警告。

>>> import urllib3
>>> http = urllib3.PoolManager()
>>> http.request("GET", "https://www.google.de")
/usr/lib/python2.7/site-packages/urllib3/connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
<urllib3.response.HTTPResponse object at 0x7f5251466c90>
>>> 

Can somebody please help me getting this fixed?有人可以帮我解决这个问题吗?

Unfortunately I have to use a API that is apparently using urllib3 under the hood to do the actual REST calls.不幸的是,我必须使用 API 显然在后台使用 urllib3 来执行实际的 REST 调用。

So I have to get it fixed w/o avoiding urllib3 module.所以我必须在不使用 urllib3 模块的情况下修复它。 I've already checked the ca certificates using ssl.SSLContext.get_ca_certs() which contains the CA certificate.我已经使用包含 CA 证书的ssl.SSLContext.get_ca_certs()检查了 ca 证书。 Doing the same with curl or openssl, works without any verification warnings.对 curl 或 openssl 执行相同操作,不会出现任何验证警告。

Thanks in advance.提前致谢。

The urllib3 docs explain how to explicitly specify a certificate bundle. urllib3 文档解释了如何明确指定证书包。 You just have to pass the path to your certificates when you initialize PoolManager() :初始化PoolManager()时,您只需传递证书的路径:

import urllib3

http = urllib3.PoolManager(
    cert_reqs="CERT_REQUIRED",
    ca_certs="/path/to/your/certificate_bundle"
)
resp = http.request("GET", "https://example.com")

By default it uses the certifi certificate bundle, so you shouldn't even have to do this unless you are using self-signed certificates or a private CA.默认情况下,它使用 certifi 证书捆绑包,因此除非您使用自签名证书或私有 CA,否则您甚至不必这样做。 If you are seeing this problem with popular sites, something is wrong with your CA related environment variables or your certifi bundle, or you are hitting a bug.如果您在流行站点上看到此问题,则说明您的 CA 相关环境变量或您的证书包有问题,或者您遇到了错误。 Upgrade to the latest versions of certifi and urllib3.升级到最新版本的 certifi 和 urllib3。 Some CA related behavior has also changed in recent versions.在最近的版本中,一些与 CA 相关的行为也发生了变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM