我创建了一个PKI处理程序来处理请求,因此可以在urllib2库中使用它。
import httplib, urllib2class 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, timeout=timeout)
要使用此功能,您将需要在处理程序中使用cookiejar。
from cookielib import cookieJarcookiejar = cookieJay()handlers = []handlers.append(HTTPSClientAuthHandler(somekey, somecert))handlers.append(urllib2.HTTPcookieProcessor(cookiejar))opener = urllib2.build_opener(*handlers)... do other urllib2 calls ....
希望这对大家有帮助!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)