Python:使用urllib3超越SSL验证

时间:2014-06-10 08:54:06

标签: python python-2.7 ssl urllib urllib3

连接SSL服务器会引发错误:

  

错误:14090086:SSL例程:SSL3_GET_SERVER_CERTIFICATE:证书   验证失败

我正在使用此urllib3 python库连接到服务器,使用此方法:

urllib3.connectionpool.connection_from_url(['remote_server_url'], maxsize=2)

如何忽略SSL验证?或者还有另一步这样做吗?

感谢。

2 个答案:

答案 0 :(得分:1)

您应该能够通过覆盖任何ConnectionPool实例的静态ConnectionCls属性来强制urllib3使用未验证的HTTPSConnection对象。例如:

from urllib3.connection import UnverifiedHTTPSConnection
from urllib3.connectionpool import connection_from_url

# Get a ConnectionPool object, same as what you're doing in your question
http = connection_from_url(remote_server_url, maxsize=2)

# Override the connection class to force the unverified HTTPSConnection class
http.ConnectionCls = UnverifiedHTTPSConnection

# Make requests as normal...
r = http.request(...)

如需其他参考,您可以check some of our tests执行类似的覆盖。

opened an issue to improve our documentation关于如何以及何时应该这样做。如果您愿意贡献,我们始终非常感谢拉取请求。 :)

答案 1 :(得分:0)

您也可以安装可能有帮助的证书库

pip install certifi