This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Unsupported provider

classification
Title: urllib2 should support HTTPS connections with client keys
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: jjlee, marcelo_fernandez, orsenthil, pitrou
Priority: normal Keywords: easy, patch

Created on 2008-07-29 14:53 by marcelo_fernandez, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg70397 - (view) Author: Marcelo Fernández (marcelo_fernandez) Date: 2008-07-29 14:53
Despite httplib does have support for HTTPS with client keys[1], urllib2
should support them too, because of the added value that urllib2 has,
like cookielib automatic handling.

[1]: http://code.activestate.com/recipes/117004/

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
msg86828 - (view) Author: Marcelo Fernández (marcelo_fernandez) Date: 2009-04-29 22:07
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
msg125566 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-01-06 16:53
In 3.2, you can instantiate a HTTPSHandler with a custom SSLContext, on which you can load your client cert:
http://docs.python.org/dev/library/urllib.request.html#urllib.request.HTTPSHandler
http://docs.python.org/dev/library/ssl.html#ssl.SSLContext.load_cert_chain
History
Date User Action Args
2022-04-11 14:56:37adminsetgithub: 47716
2011-01-06 16:53:53pitrousetstatus: open -> closed

versions: + Python 3.2, - Python 2.7
nosy: + pitrou

messages: + msg125566
resolution: out of date
stage: test needed ->
2009-04-29 22:07:03marcelo_fernandezsetmessages: + msg86828
2009-04-22 17:23:21ajaksu2setpriority: normal
keywords: + patch, easy
2009-02-13 01:56:02ajaksu2setnosy: + jjlee
stage: test needed
versions: + Python 2.7
2008-08-08 00:26:32orsenthilsetnosy: + orsenthil
2008-07-29 14:53:34marcelo_fernandezcreate