Joakim
Joakim

Reputation: 11998

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.

Upvotes: 52

Views: 96166

Answers (2)

Tushar Gupta
Tushar Gupta

Reputation: 95

You can specify cert_reqs='CERT_NONE' while creating PoolManager or ProxyManager objects.

For example:

proxy = urllib3.ProxyManager("https://localhost:8443", cert_reqs='CERT_NONE')

or

pool = urllib3.PoolManager(cert_reqs='CERT_NONE')

Upvotes: -1

Muhammad Tahir
Muhammad Tahir

Reputation: 5184

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.

Upvotes: 5

Related Questions