Python:通过urllib3超越SSL验证
连接到SSL服务器时出现了一个错误:
错误信息:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:证书验证失败
我正在使用这个urllib3的Python库来连接服务器,使用的方法是:
urllib3.connectionpool.connection_from_url(['remote_server_url'], maxsize=2)
我该如何忽略SSL验证?或者还有其他步骤吗?
谢谢。
2 个回答
0
你也可以安装证书库,这可能会有所帮助。
pip install certifi
1
你可以通过覆盖任何连接池实例的静态 ConnectionCls
属性,强制 urllib3 使用一个未验证的 HTTPS 连接对象。比如说:
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(...)
如果你想了解更多,可以查看我们的一些测试,这些测试做了类似的覆盖。
我已经提出了一个问题,希望改善我们的文档,说明应该如何以及何时进行这样的操作。如果你想贡献代码,我们总是很欢迎的。:)