Mark McWiggins
Mark McWiggins

Reputation: 651

Curl-like sending of client certificates without PyCurl: how?

Our client wants a client script that will be installed on their customers' computers to be as trivial to install as possible. This means no extra-install packages, in this case PyCurl.

We need to be able to connect to a website using SSL and expecting a client certificate. Currently this is done calling Curl with os.system() but to get the http return code doing this it looks like we'll have to use the '-v' option to Curl and comb through this output. Not difficult, just a bit icky.

Is there some other way to do this using the standard library that comes with Python 2.6? I read everything I could find on this and I couldn't see a non-Curl way of doing it.

Thanks in advance for any guidance on this subject whatsoever!

Upvotes: 2

Views: 1199

Answers (1)

Dariusz Suchojad
Dariusz Suchojad

Reputation: 194

this will do the trick. Note that Verisign don't require a client certificate, it's just a randomly taken HTTPS site.

import httplib

conn = httplib.HTTPSConnection('verisign.com', key_file='./my-key.pem', cert_file='./my-cert.pem')

conn.connect()
conn.request('GET', '/')

conn.set_debuglevel(20)

response = conn.getresponse()
print('HTTP status', response.status)

EDIT: Just for the posterity, Bruno's comment below is a valid one and here's an article how to roll it using the stdlib's socket ssl and socket modules in case it's needed.

EDIT2: Seems I cannot post links - just do a web search for 'Validating SSL server certificate with Python 2.x another day'

Upvotes: 3

Related Questions