Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 urllib ignore SSL certificate verification

I have a server setup for testing, with a self-signed certificate, and want to be able to test towards it.

How do you ignore SSL verification in the Python 3 version of urlopen?

All information I found regarding this is regarding urllib2 or Python 2 in general.

urllib in python 3 has changed from urllib2:

Python 2, urllib2: urllib2.urlopen(url[, data[, timeout[, cafile[, capath[, cadefault[, context]]]]])

https://docs.python.org/2/library/urllib2.html#urllib2.urlopen

Python 3: urllib.request.urlopen(url[, data][, timeout]) https://docs.python.org/3.0/library/urllib.request.html?highlight=urllib#urllib.request.urlopen

So I know this can be done in Python 2 in the following way. However Python 3 urlopen is missing the context parameter.

import urllib2
import ssl

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

urllib2.urlopen("https://your-test-server.local", context=ctx)

And yes I know this is a bad idea. This is only meant for testing on a private server.

I could not find how this is supposed to be done in the Python 3 documentation, or in any other question. Even the ones explicitly mentioning Python 3, still had a solution for urllib2/Python 2.

like image 331
Joakim Avatar asked Apr 13 '16 13:04

Joakim


2 Answers

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.

like image 179
nngeek Avatar answered Nov 13 '22 12:11

nngeek


Python 3.0 to 3.3 does not have context parameter, It was added in Python 3.4. So, you can update your Python version to 3.5 to use context.

like image 6
Muhammad Tahir Avatar answered Nov 13 '22 12:11

Muhammad Tahir