我使用最新的Kubuntu
和Python 2.7.6
。我尝试使用以下代码下载https
页面:
import urllib2
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'pl-PL,pl;q=0.8',
'Connection': 'keep-alive'}
req = urllib2.Request(main_page_url, headers=hdr)
try:
page = urllib2.urlopen(req)
except urllib2.HTTPError, e:
print e.fp.read()
content = page.read()
print content
然而,我收到了这样的错误:
Traceback (most recent call last):
File "test.py", line 33, in <module>
page = urllib2.urlopen(req)
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 404, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 422, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1222, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1184, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 1] _ssl.c:510: error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal error>
如何解决这个问题?
解决!
我使用了@SteffenUllrich给出的网址https://www.ssllabs.com。原来,服务器使用TLS 1.2,所以我将python更新为2.7.10并将我的代码修改为:
import ssl
import urllib2
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'pl-PL,pl;q=0.8',
'Connection': 'keep-alive'}
req = urllib2.Request(main_page_url, headers=hdr)
try:
page = urllib2.urlopen(req,context=context)
except urllib2.HTTPError, e:
print e.fp.read()
content = page.read()
print content
现在下载页面。
答案 0 :(得分:5)
我使用最新的Kubuntu和Python 2.7.6
据我所知,最新的Kubuntu(15.10)使用2.7.10。但假设您使用的是包含在14.04 LTS中的2.7.6:
也适用于我,所以这可能是页面问题。现在怎么办?
然后它取决于网站。此版本的Python的典型问题是缺少对Server Name Indication (SNI)的支持,而multiple trust path仅添加到Python 2.7.9中。由于许多网站今天都需要SNI(就像使用Cloudflare Free SSL的所有网站一样),我想这就是问题所在。
但是,还有其他可能性,例如SSLLabs,只能通过OpenSSL 1.0.2修复。或者只是缺少中间证书等。只有在您提供URL或根据此信息和{{3}}的分析自行分析情况时,才可能提供更多信息,也可能是解决方法。
答案 1 :(得分:1)
旧版本的python 2.7.3 使用
requests.get(download_url, headers=headers, timeout=10, stream=True)
获取以下警告和异常:
You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
SSLError(SSLError(1, '_ssl.c:504: error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal error')
只需遵循建议,即可访问 Certificate verification in Python 2
运行
pip install urllib3[secure]
问题解决了。
答案 2 :(得分:0)
以上答案仅部分正确,您可以添加修复程序来解决此问题:
代码:
def allow_unverified_content():
"""
A 'fix' for Python SSL CERTIFICATE_VERIFY_FAILED (mainly python 2.7)
"""
if (not os.environ.get('PYTHONHTTPSVERIFY', '') and
getattr(ssl, '_create_unverified_context', None)):
ssl._create_default_https_context = ssl._create_unverified_context
不加选择地调用它:
allow_unverified_content()