SSL_CERTIFICATE_FAILED错误+公司代理urrlib2

时间:2016-12-13 21:07:34

标签: python ssl urllib2

我无法使用HTTPS访问urllib2个网站。这就是我所拥有的:

import urllib2 
import ssl

proxy = urllib2.ProxyHandler({'https':'http://username:password@proxy:port',
                              'http': 'http://username:password@proxy:port'})
opener = urllib2.build_opener(urllib2.HTTPHandler(), 
                              urllib2.HTTPSHandler(), 
                              proxy)
urllib2.install(opener)

url_secure = 'https://www.google.com'
url_nonsecure = 'http://www.google.com'

response = urllib2.urlopen(url_nonsecure)
d = response.read()
response.close()
print d

以上运行没有问题。但是,如果我尝试使用

运行上述内容
response = urllib2.urlopen(url_secure)

我得到了

Traceback (most recent call last):
File "google_maps.py", line 25, in <module>
response = urllib2.urlopen(url_secure)
File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 429, in open
response = self._open(req, data)
File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 447, in _open
'_open', req)
File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 1241, in https_open
context=self._context)
File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 1198, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>

thisthis,似乎可以通过创建新的上下文来解决问题。所以我试过了两个:

ctx=ssl._create_unverified_context()

ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)

然后跑

response = urllib2.urlopen(url_secure, context = ctx)
d = response.read()
response.close()
print d

这两个都引发了以下错误:

Traceback (most recent call last):
File "google_maps.py", line 25, in <module>
response = urllib2.urlopen(url_secure, context=ctx)
File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 429, in open
response = self._open(req, data)
File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 447, in _open
'_open', req)
File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 1241, in https_open
context=self._context)
File "/Users/username/anaconda2/lib/python2.7/urllib2.py", line 1198, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>

如何访问https网站?

0 个答案:

没有答案
相关问题