Issue3466
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) * ![]() |
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:37 | admin | set | github: 47716 |
| 2011-01-06 16:53:53 | pitrou | set | status: open -> closed versions:
+ Python 3.2, - Python 2.7 messages:
+ msg125566 |
| 2009-04-29 22:07:03 | marcelo_fernandez | set | messages: + msg86828 |
| 2009-04-22 17:23:21 | ajaksu2 | set | priority: normal keywords: + patch, easy |
| 2009-02-13 01:56:02 | ajaksu2 | set | nosy:
+ jjlee stage: test needed versions: + Python 2.7 |
| 2008-08-08 00:26:32 | orsenthil | set | nosy: + orsenthil |
| 2008-07-29 14:53:34 | marcelo_fernandez | create | |
