代码如下:
import urllib2 as u2
from suds.client import Client
from suds.transport.http import HttpTransport, Reply, TransportError
import httplib
class HTTPSClientAuthHandler(u2.HTTPSHandler):
def __init__(self, key, cert):
u2.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)
class HTTPSClientCertTransport(HttpTransport):
def __init__(self, key, cert, *args, **kwargs):
HttpTransport.__init__(self, *args, **kwargs)
self.key = key
self.cert = cert
def u2open(self, u2request):
"""
Open a connection.
@param u2request: A urllib2 request.
@type u2request: urllib2.Requet.
@return: The opened file-like urllib2 object.
@rtype: fp
"""
tm = self.options.timeout
url = u2.build_opener(HTTPSClientAuthHandler(self.key, self.cert))
if self.u2ver() < 2.6:
socket.setdefaulttimeout(tm)
return url.open(u2request)
else:
return url.open(u2request, timeout=tm)
def getClient(url, key, cert):
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)
c = Client(url,
transport=HTTPSClientCertTransport(key, cert))
return c
def websevice(url):
key = '/blablabla/test.key' #这里换成你调用端的私钥文件
cert = '/blablabla/123.cer' #这里换成你要调用的网址的客户端证书文件
client = getClient(url, key, cert)
result = client.service
return result
调用:
websevice(https://地址)
结果:
DEBUG:suds.transport.http:opening
这是什么原因呢?
我手机上没法测,不过你这个很容易看出来,你需要确认client.service是不是访问的结果。很明显不是,那么这个result有什么属性看一下,比如result.content或者response或者body什么。回公司我试下。