我使用的是 python 2.7.10
request = urllib2.Request(url, data=urllib.urlencode(params))
f = urllib2.urlopen(request))
导致以下异常:
urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)
URL 是一个在 IIS 中托管的网站,我们的组织已签署证书。 我已经将根证书导入到Windows证书管理器中,并且我能够在浏览器中安全地打开URL而不会遇到诸如“此网站的安全证书存在问题”之类的消息”
如何解决此问题? 我不想禁用 SSL 验证
最佳答案
当您通过浏览器访问 URL 时,您的浏览器将成为客户端,而服务器将成为托管网站的服务器。现在,由于您已经为浏览器导入了 CA 证书,这就是为什么网站可以在浏览器中正常打开的原因。
现在,当您从 python 脚本打开同一网站时,客户端将成为您的 python 脚本,并且它不知道此 CA 证书。 Python 脚本不使用Windows 证书存储区,因此您必须指定一个CA 证书来完成证书验证。
因此,您必须明确告诉有关 CA 证书的脚本,如下所示:
urllib2.urlopen("https://dinesh.com", cafile="test_cert.pem")
您可以在此处找到文档:urllib2.urlopen 。
更新
上述链接的片段:
The optional
cafile
andcapath
parameters specify a set of trusted CA certificates for HTTPS requests.cafile
should point to a single file containing a bundle of CA certificates, whereascapath
should point to a directory of hashed certificate files. More information can be found in ssl.SSLContext.load_verify_locations().
关于python - SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45408850/