Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python ignore certificate validation urllib2

I want to ignore the certification validation during my request to the server with an internal corporate link.

With python requests library I would do this:

r = requests.get(link, allow_redirects=False,verify=False) 

How do I do the same with urllib2 library?

like image 868
Sangamesh Hs Avatar asked Oct 09 '13 09:10

Sangamesh Hs


2 Answers

In the meantime urllib2 seems to verify server certificates by default. The warning, that was shown in the past disappeared for 2.7.9 and I currently ran into this problem in a test environment with a self signed certificate (and Python 2.7.9).

My evil workaround (don't do this in production!):

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) 

According to docs calling SSLContext constructor directly should work, too. I haven't tried that.

like image 193
Enno Gröper Avatar answered Oct 06 '22 07:10

Enno Gröper


The easiest way:

python 2

import urllib2, ssl  request = urllib2.Request('https://somedomain.co/') response = urllib2.urlopen(request, context=ssl._create_unverified_context()) 

python 3

from urllib.request import urlopen import ssl  response = urlopen('https://somedomain.co', context=ssl._create_unverified_context()) 
like image 38
Kron Avatar answered Oct 06 '22 05:10

Kron