Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable SSL verification for urlretrieve?

Tags:

python

urllib2

A similar question had been asked a couple times around SO, but the solutions are for urlopen. That function takes an optional context parameter which can accept a pre-configured SSL context. urlretrieve does not have this parameter. How can I bypass SSL verification errors in the following call?

urllib.request.urlretrieve(
    "http://sourceforge.net/projects/libjpeg-turbo/files/1.3.1/libjpeg-turbo-1.3.1.tar.gz/download", 
    destFolder+"/libjpeg-turbo.tar.gz")
like image 233
Violet Giraffe Avatar asked Apr 04 '17 10:04

Violet Giraffe


People also ask

How do I disable SSL certificate verification in Python?

To disable certificate verification, at the client side, one can use verify attribute.

How do I disable SSL https?

In the Internet Options window on the Advanced tab, under Settings, scroll down to the Security section. In the Security section, locate the Use SSL and Use TLS options and uncheck Use SSL 3.0 and Use SSL 2.0.

What is SSL certificate verify failed?

An SSL certificate error occurs when a web browser can't verify the SSL certificate installed on a site. Rather than connect users to your website, the browser will display an error message, warning users that the site may be insecure.


2 Answers

This solution worked as well for me: before making the call to the library, define the default SSL context:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context
# urllib.request.urlretrieve(...)

Source: http://thomas-cokelaer.info/blog/2016/01/python-certificate-verified-failed/

like image 184
Thierry Avatar answered Nov 16 '22 03:11

Thierry


This does not appear to be possible with urlretrieve (in Python >=2.7.9, or Python >=3.0).

The requests package is recommended as a replacement.

Edited to add: the context parameter has been added to the code, even though it isn't mentioned in the documentation! Hat-tip @Sushisource

like image 24
Oddthinking Avatar answered Nov 16 '22 04:11

Oddthinking