搜索
您的当前位置:首页正文

关于 Python 3.6 及以上发送 HTTPS 请求的证书验

来源:知库网

macOS 10.12.5

在 Python 3.6 及以上的版本中,直接发送 HTTPS 请求,会出现以下问题

[SSL: CERTIFICATE_VERIFY_FAILED]

这个问题兜一圈才注意到 Python 安装目录下 ReadMe.rtf 中说明了这个问题

Certificate verification and OpenSSL

The bundled pip included with the Python 3.6 installer has its own default certificate store for verifying download connections.

也可以在支持选择证书文件的方法中手动加入,如

import urllib
import certifi

request = 
urllib.request.urlopen(request, cafile=certifi.where())

另外还有一个不推荐的方法解决这个问题
使用不验证 SSL 证书的方式发送请求

import ssl

urllib.request.urlopen(request, context=ssl._create_unverified_context())
Top