Problem urllib2 with HTTPS

3

I'm trying to retrieve a page on Flickr using urllib2 and I get the following error when I execute the command urllib2.urlopen('https://www.flickr.com')

File "/usr/lib/python2.7/urllib2.py", line 104, in urlopen
    return _opener.open(url, data, timeout)   
File "/usr/lib/python2.7/urllib2.py", line 361, in open
    response = self._open(req, data)   
File "/usr/lib/python2.7/urllib2.py", line 379, in _open
    '_open', req)   
File "/usr/lib/python2.7/urllib2.py", line 339, in _call_chain
    result = func(*args)   
File "/usr/lib/python2.7/urllib2.py", line 1099, in https_open
    return self.do_open(httplib.HTTPSConnection, req)   
File "/usr/lib/python2.7/urllib2.py", line 1066, in do_open
    raise URLError(err) urllib2.URLError: <urlopen error [Errno 111] Connection refused>

Apparently the problem occurs when I connect with HTTPS because with HTTP it works. FLickr uses TLS 1.2. I do not know if it's a version or SSL problem.

Does anyone know? I need to connect through HTTPS. Thanks

    
asked by Nico Rossello 21.04.2016 в 22:08
source

2 answers

1

Surely you will have to specify the SSL protocol that you want to use. It can be passed as context to urllib2.urlopen :

import ssl
import urllib2

context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
req = urllib2.urlopen('https://www.flickr.com', context=context)

Edited

Also make sure you are using the latest revision of python-2.7 (minimum python-2.7.9). The python2 SSL library has serious security flaws and only the latest patches receive the patches created from python3.

    
answered by 22.04.2016 в 11:03
0

Simply ping link to try not to have problems opening the url, if you are under a proxy server the problem is probably because it prevents you from accessing, or you're offline.

deal with this way:

import urllib2

req = urllib2.Request('https://www.flickr.com')
response = urllib2.urlopen(req)
contenido = response.read()
print contenido 
    
answered by 21.04.2016 в 22:46