簡體   English   中英

使用 urllib 下載 HTTPS 頁面,錯誤:14077438:SSL 例程:SSL23_GET_SERVER_HELLO:tlsv1 警報內部錯誤

[英]Downloading HTTPS pages with urllib, error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal error

我使用最新的KubuntuPython 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

現在它下載頁面。

我使用最新的 Kubuntu 和 Python 2.7.6

據我所知,最新的 Kubuntu(15.10)使用 2.7.10。 但假設您使用 14.04 LTS 中包含的 2.7.6:

對我來說也適用於 facebook,所以這可能是頁面問題。 現在怎么辦?

然后這取決於網站。 此版本 Python 的典型問題是缺少對僅添加到 Python 2.7.9 的服務器名稱指示 (SNI) 的支持。 由於今天許多站點都需要 SNI(就像使用 Cloudflare Free SSL 的所有站點一樣),我想這就是問題所在。

但是,還有其他可能性,例如僅使用 OpenSSL 1.0.2 修復的多信任路徑 或者只是缺少中間證書等。只有提供 URL 或根據此信息和SSLLabs的分析自己分析情況,才能提供更多信息和解決方法。

舊版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')

在此處輸入圖片說明

只需按照建議,訪問Python 2 中的證書驗證

pip install urllib3[secure]

並解決了問題。

以上答案僅部分正確,您可以添加修復程序來解決此問題:

代碼:

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM