【问题标题】:Error urllib2 Python. SSL: TLSV1_ALERT_INTERNAL_ERROR ssl.c:590错误 urllib2 Python。 SSL:TLSV1_ALERT_INTERNAL_ERROR ssl.c:590
【发布时间】:2015-12-30 02:40:10
【问题描述】:

我正在尝试使用 urllib for Python 打开一个 URL,但是我收到一个错误,其中一个特定的 URL 与其他 URL 相比看起来很正常,并且在浏览器中也能正常工作。

产生错误的代码是:

import cStringIO
import imghdr
import urllib2
response = urllib2.urlopen('https://news.artnet.com/wp-content/news-upload/2015/08/Brad_Pitt_Fury_2014-e1440597554269.jpg')

但是,如果我使用相似的 URL 执行完全相同的操作,我不会收到错误消息:

import cStringIO
import imghdr
import urllib2
response = urllib2.urlopen('https://upload.wikimedia.org/wikipedia/commons/d/d4/Brad_Pitt_June_2014_(cropped).jpg')

我在第一个例子中得到的错误是:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1240, in https_open
    context=self._context)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1197, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:590)>

【问题讨论】:

  • 你能解决这个问题吗?我在访问 URL 时也遇到了同样的问题。

标签: python ssl urllib2


【解决方案1】:

这是一个使用 Cloudflare SSL 的站点,它需要 Server Name Indication (SNI)。如果没有 SNI 访问此站点将显示您可以在此处看到的行为,即触发 tlsv1 警报。 SNI 仅在 2.7.9 中添加到 Python 2.7,您可能使用的是旧版本的 Python。

【讨论】:

  • 我的 Python 版本似乎是 2.7.10:Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12) [GCC 4.2.1 (Apple Inc. build 5666) ) (dot 3)] 关于达尔文
  • @user2348684:可能 urllib2 还不支持 SNI(SNI 需要被库显式使用)。更好地尝试肯定应该得到支持的请求。
  • 这是这个问题的真正答案。我很惊讶它没有被标记为它。
【解决方案2】:

我在 urllib3 文档中找到了一个很好的解释。

以下代码解决了 Python 2.7.10 的问题:

import urllib3 
import ssl 
import certifi
urllib3.contrib.pyopenssl.inject_into_urllib3()

# Open connection
http = urllib3.PoolManager(
    cert_reqs='CERT_REQUIRED', # Force certificate check.
    ca_certs=certifi.where(),  # Path to the Certifi bundle.
)

# Make verified HTTPS requests. 
try:
    resp = http.request('GET', url_photo) 
except urllib3.exceptions.SSLError as e:
    # Handle incorrect certificate error.
    print "error with https certificates"

【讨论】:

  • 想链接到解释吗?
猜你喜欢
  • 1970-01-01
  • 2015-03-04
  • 2016-10-22
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多