Issue 3466: urllib2 should support HTTPS connections with client keys (original) (raw)
Despite httplib does have support for HTTPS with client keys1, urllib2 should support them too, because of the added value that urllib2 has, like cookielib automatic handling.
However, I made a workaround:
#-- coding: utf-8 --
import httplib import urllib2
key_file = None cert_file = None
class HTTPSClientAuthConnection(httplib.HTTPSConnection): def init(self, host): httplib.HTTPSConnection.init(self, host, key_file=key_file, cert_file=cert_file)
class HTTPSClientAuthHandler(urllib2.HTTPSHandler): def https_open(self, req): return self.do_open(HTTPSClientAuthConnection, req)
Regards, Marcelo
The workaround I posted before doesn't work with Python 2.6. This one works (at least) with Python 2.5 and Python 2.6:
import httplib import urllib2
key_file = 'mykey.pem' cert_file = 'mycert-signed.pem'
class HTTPSClientAuthConnection(httplib.HTTPSConnection): def init(self, host, timeout=None): httplib.HTTPSConnection.init(self, host, key_file=key_file, cert_file=cert_file) self.timeout = timeout # Only valid in Python 2.6
class HTTPSClientAuthHandler(urllib2.HTTPSHandler): def https_open(self, req): return self.do_open(HTTPSClientAuthConnection, req)
This is a little class to use it:
class Connection(object):
def __init__(self, url):
# TODO: Validate/Sanitize the url
self.url = url
self._cookiejar = cookielib.CookieJar()
def send(self, **data):
parameters = urllib.urlencode(data)
opener =
urllib2.build_opener(HTTPHandler(debuglevel=DEBUG_LEVEL), HTTPSClientAuthHandler(debuglevel=DEBUG_LEVEL), HTTPCookieProcessor(self._cookiejar)) req = Request(self.url, parameters) server_response = opener.open(req).read() print server_response return server_response
Regards