Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Forcing TLS in Python's urllib2
# Python 2.6's urllib2 does not allow you to select the TLS dialect,
# and by default uses a SSLv23 compatibility negotiation implementation.
# Besides being vulnerable to POODLE, the OSX implementation doesn't
# work correctly, failing to connect to servers that respond only to
# TLS1.0+. These classes help set up TLS support for urllib2.
class TLS1Connection(httplib.HTTPSConnection):
"""Like HTTPSConnection but more specific"""
def __init__(self, host, **kwargs):
httplib.HTTPSConnection.__init__(self, host, **kwargs)
def connect(self):
"""Overrides HTTPSConnection.connect to specify TLS version"""
# Standard implementation from HTTPSConnection, which is not
# designed for extension, unfortunately
sock = socket.create_connection((self.host, self.port),
self.timeout, self.source_address)
if getattr(self, '_tunnel_host', None):
self.sock = sock
self._tunnel()
# This is the only difference; default wrap_socket uses SSLv23
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file,
ssl_version=ssl.PROTOCOL_TLSv1)
class TLS1Handler(urllib2.HTTPSHandler):
"""Like HTTPSHandler but more specific"""
def __init__(self):
urllib2.HTTPSHandler.__init__(self)
def https_open(self, req):
return self.do_open(TLS1Connection, req)
# Override default handler
urllib2.install_opener(urllib2.build_opener(TLS1Handler()))
@OlofT

This comment has been minimized.

Copy link

@OlofT OlofT commented Nov 24, 2014

Thanks!

Note, remember to also import the following:

import httplib
import socket
import ssl

@patrickwolf

This comment has been minimized.

Copy link

@patrickwolf patrickwolf commented Nov 24, 2014

Thanks @flandr.

PS: Create a ticket with httplib to add support for specifying the SSL protocol:
https://github.com/jcgregorio/httplib2/issues/287

@IDmedia

This comment has been minimized.

Copy link

@IDmedia IDmedia commented Jan 16, 2015

Awesome. But how might I use this with cookies and modified headers?

urllib2.install_opener(urllib2.build_opener(TLS1Handler()))

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36')]

@brysontyrrell

This comment has been minimized.

Copy link

@brysontyrrell brysontyrrell commented Mar 10, 2015

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.