在urllib2中使用客户端证书
我需要在我的服务器和一个远程网络服务之间建立一个安全的通道。我会使用HTTPS,并且会用到客户端证书。同时,我还需要验证远程服务提供的证书是否正确。
我该如何在urllib2中使用我自己的客户端证书呢?
我在代码中需要做些什么来确保远程证书是正确的?
4 个回答
7
根据Antoine Pitrou在Hank Gay的回答中提到的问题,这个过程可以稍微简化一下(截至2011年),方法是使用自带的ssl
库:
import ssl
import urllib.request
context = ssl.create_default_context()
context.load_cert_chain('/path/to/file.pem', '/path/to/file.key')
opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=context))
response = opener.open('https://example.org')
print(response.read())
这是Python 3的代码,不过ssl
库在Python 2中也可以使用。
load_cert_chain
这个函数还可以接受一个可选的密码参数,这样就可以对私钥进行加密。
37
因为alex的回答是一个链接,而那个页面上的代码格式很糟糕,所以我就把这个放在这里,留作记录:
import urllib2, httplib
class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
def __init__(self, key, cert):
urllib2.HTTPSHandler.__init__(self)
self.key = key
self.cert = cert
def https_open(self, req):
# Rather than pass in a reference to a connection class, we pass in
# a reference to a function which, for all intents and purposes,
# will behave as a constructor
return self.do_open(self.getConnection, req)
def getConnection(self, host, timeout=300):
return httplib.HTTPSConnection(host, key_file=self.key, cert_file=self.cert)
opener = urllib2.build_opener(HTTPSClientAuthHandler('/path/to/file.pem', '/path/to/file.pem.') )
response = opener.open("https://example.org")
print response.read()
12
这里有一个在官方Python问题追踪网站上记录的bug,看起来和这个问题有关,并且有一个建议的修复方案。