使用 SSL 和 headers 在 python 中通过 SUDS 调用 Soap API

Call Soap API through SUDS in python with SSL and headers

我有一块肥皂 api,我想在 python 中用肥皂水来称呼它。我已经安装了肥皂水并能够连接到肥皂 api。

client = Client("http://my_sevone_appliance/soap3/api.wsdl")

但问题是我必须发送一些 headers 和额外的 headers 到 soap api 而且我还必须发送 TLSv1 中的 ssl 文件。

ssl.wrap_socket() 记住python版本是2.7

但不知何故我无法做到这一点。 有人可以帮助我如何从肥皂水中调用肥皂 api 并在其中传递 headers 以及 ssl。谢谢

我发现 solution.I 在 pythonsuds 中完成了所有这些事情。请安装 suds。

所以首先我在 python 中编写了一个 tranport.py 脚本,它实际上获取证书和密钥文件并将其传递给 suds 客户端。这是代码。

import urllib2, httplib, socket
from suds.transport.http import HttpTransport, Reply, TransportError
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)
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 = urllib2.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)

要使用上面的方法并成功调用 soap api 我使用以下代码。

url = <wsdl url of soap api eg: url ="http://my_sevone_appliance/soap3/api.wsdl">
pem = <is the key file>  
cert = <is the cert>   
c = Client(url, transport=HTTPSClientCertTransport(pem, cert))

然后我调用我想要点击的功能。你可以看到所有你想点击的功能。

print c

这里我还得用headers来送肥皂。所以我还必须通过 headers(可选)。

import ast
headers = '{}'
c.set_options(headers=ast.literal_eval(headers))

我必须使用 ast,因为你必须以 json 格式发送 headers。然后我调用函数。

xml=<the xml data  which you want to send>
example : 
 xml ='<soap:Env xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ser="http://www.abc1.com/services">
             <soap:Header/>
             <soap:Body>
                <ser:getName>
                   <ser:ver>1</ser:ver>
                   <ser:syncId>222</ser:syncID>
                   <ser:mobileNo>0987654321</ser:mobileNo>
                </ser:getBalance>
             </soap:Body>
          </soap:Env>'

现在点击肥皂功能

 c.service.getName(__inject={'msg': xml})

这里的 getName 是我在 soap 中的函数 api。

就是这样。有问题可以问我

#**If you don't have any cert file and key:**

import ast
from suds.client import Client
headers='{}'
url = <wsdl url of soap api eg: url ="http://my_sevone_appliance/soap3/api.wsdl">
client = Client(url,location=url)
client.options.cache.clear()
c.set_options(headers=ast.literal_eval(headers))
c=client.service.<service name>(__inject={'msg': xml})    #eg. c.service.getName(__inject={'msg': xml})


#**fetching data from response**
#for example if the output will:
# c.CustomerInfo
#(ArrayOfCustomer){
#   Customer[] = 
#      (Customer){
#         Account = xyz
#         AccountType = abc
#         Account_TypeID = mno
#         Address = None
#         Address2 = None
#         City = Pune
#},
#}

#then fetch like:  c.CustomerInfo.Customer[0].Account