Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error urllib2 Python. SSL: TLSV1_ALERT_INTERNAL_ERROR ssl.c:590

I am trying to use urllib for Python to open a URL but I get an error with one specific URL which looks normal compared to the others and also works well in a browser.

The code that generates the error is:

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

However, if I do the exact same thing with a similar URL I don't get the error:

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

The error I get in the first example is:

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)>
like image 779
user2348684 Avatar asked Sep 26 '22 19:09

user2348684


2 Answers

This is a site using Cloudflare SSL and it needs Server Name Indication (SNI). Without SNI access to this site will show the behavior you can see here, i.e. trigger a tlsv1 alert. SNI was added to Python 2.7 only with 2.7.9 and you are probably using an older version of Python.

like image 185
Steffen Ullrich Avatar answered Oct 20 '22 07:10

Steffen Ullrich


I found a very good explanation in the urllib3 documentation.

The following code solved the issue with 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"
like image 22
user2348684 Avatar answered Oct 20 '22 09:10

user2348684