我尝试使用urlopen连接到内部网站。无论是否提供咖啡馆,它都会因SSL错误而反复失败。
我尝试了stackoverflow答案中解释的所有各种方法。但没有运气。
urllib2.urlopen(url,cafile=certifi.where())
第二种方式:
context = ssl.create_default_context(cafile=certifi.where())
urllib2.urlopen(url,context=context)
第三种方式:
ctx = ssl.create_default_context()
ctx.load_verify_locations(cafile = certifi.where())
urllib2.urlopen(url,context=ctx)
无论哪种方式,我尝试,我都会收到以下错误。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "urllib2.py", line 429, in open
response = self._open(req, data)
File "urllib2.py", line 447, in _open
'_open', req)
File "urllib2.py", line 407, in _call_chain
result = func(*args)
File "urllib2.py", line 1241, in https_open
context=self._context)
File "urllib2.py", line 1198, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)>
我已将CA证书的条目添加到文件certifi.where()中。我使用的是python版本2.7.14。
有人能告诉我在这里是否遗漏了什么?或者我使用的python版本不支持它?另外,让我知道调试方法,即找出CA证书是否存在任何问题。
感谢。
编辑:我不想按照urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error中的一个答案中的建议选择退出SSL验证。另一个答案告诉我使用urlopen咖啡馆,这在我的情况下不起作用。我已经尝试过这个问题的答案中给出的解决方案;但没有运气。
此外,openssl会抛出以下错误。
[root@host1 ~]# openssl s_client -connect url -CAfile "cacert.pem"
...
Certificate chain
...
Server certificate
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
...
No client certificate CA names sent
...
SSL handshake has read 2098 bytes and written 415 bytes
...
Verify return code: 2 (unable to get issuer certificate)
---
HTTP/1.0 408 Request Time-out
Cache-Control: no-cache
Connection: close
Content-Type: text/html
<html><body><h1>408 Request Time-out</h1>
Your browser didn't send a complete request in time.
</body></html>
closed