13

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")
Oddthinking
  • 24,359
  • 19
  • 83
  • 121
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335

2 Answers2

21

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/

Thierry
  • 226
  • 2
  • 3
1

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

Oddthinking
  • 24,359
  • 19
  • 83
  • 121
  • This is possible at least as of 2.7.14 – Sushisource Feb 06 '18 at 21:01
  • @Sushisource: I can't see any evidence that is true, but I would love to be proven wrong. Please post a new answer with a link to some documentation or code that backs it up. – Oddthinking Feb 06 '18 at 23:06
  • It's not in the docs, but the urlretrieve function happily accepted a context parameter in 2.7.14 – Sushisource Feb 09 '18 at 00:45
  • Wow, you are right. It was [added 3 years ago](https://github.com/python/cpython/commit/b206473ef8a7abe9abf5ab8776ea3bcb90adc747) but the documentation doesn't seem to have been updated. – Oddthinking Feb 09 '18 at 01:18