Python 3 urllib ignore SSL certificate verification

The accepted answer just gave advise to use python 3.5+, instead of direct answer. It causes confusion.

For someone looking for a direct answer, here it is:

import ssl
import urllib.request

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

with urllib.request.urlopen(url_string, context=ctx) as f:
    f.read(300)

Alternatively, if you use requests library, it has much better API:

import requests

with open(file_name, 'wb') as f:
    resp = requests.get(url_string, verify=False)
    f.write(resp.content)

The answer is copied from this post (thanks @falsetru): How do I disable the ssl check in python 3.x?

These two questions should be merged.

Leave a Comment