鍍金池/ 問答/Python  HTML/ suds請求https報錯

suds請求https報錯

代碼如下:

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' #這里換成你調(diào)用端的私鑰文件
    cert = '/blablabla/123.cer' #這里換成你要調(diào)用的網(wǎng)址的客戶端證書文件
    client = getClient(url, key, cert)
    result = client.service
    return result


調(diào)用:
websevice(https://地址)

結(jié)果:
DEBUG:suds.transport.http:opening

這是什么原因呢?

回答
編輯回答
終相守

我手機(jī)上沒法測,不過你這個很容易看出來,你需要確認(rèn)client.service是不是訪問的結(jié)果。很明顯不是,那么這個result有什么屬性看一下,比如result.content或者response或者body什么?;毓疚以囅?。

2017年2月10日 09:55