Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

urllib2 should support HTTPS connections with client keys #47716

Closed
marcelofernandez mannequin opened this issue Jul 29, 2008 · 3 comments
Closed

urllib2 should support HTTPS connections with client keys #47716

marcelofernandez mannequin opened this issue Jul 29, 2008 · 3 comments
Labels
easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@marcelofernandez
Copy link
Mannequin

marcelofernandez mannequin commented Jul 29, 2008

BPO 3466
Nosy @orsenthil, @pitrou

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2011-01-06.16:53:53.323>
created_at = <Date 2008-07-29.14:53:34.764>
labels = ['easy', 'type-feature', 'library']
title = 'urllib2 should support HTTPS connections with client keys'
updated_at = <Date 2011-01-06.16:53:53.320>
user = 'https://bugs.python.org/marcelofernandez'

bugs.python.org fields:

activity = <Date 2011-01-06.16:53:53.320>
actor = 'pitrou'
assignee = 'none'
closed = True
closed_date = <Date 2011-01-06.16:53:53.323>
closer = 'pitrou'
components = ['Library (Lib)']
creation = <Date 2008-07-29.14:53:34.764>
creator = 'marcelo_fernandez'
dependencies = []
files = []
hgrepos = []
issue_num = 3466
keywords = ['patch', 'easy']
message_count = 3.0
messages = ['70397', '86828', '125566']
nosy_count = 4.0
nosy_names = ['jjlee', 'orsenthil', 'pitrou', 'marcelo_fernandez']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue3466'
versions = ['Python 3.2']

@marcelofernandez
Copy link
Mannequin Author

marcelofernandez mannequin commented Jul 29, 2008

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

@marcelofernandez marcelofernandez mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jul 29, 2008
@devdanzin devdanzin mannequin added the easy label Apr 22, 2009
@marcelofernandez
Copy link
Mannequin Author

marcelofernandez mannequin commented Apr 29, 2009

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

@pitrou
Copy link
Member

pitrou commented Jan 6, 2011

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

@pitrou pitrou closed this as completed Jan 6, 2011
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant