【问题标题】:Call Soap API through SUDS in python with SSL and headers使用 SSL 和标头在 python 中通过 SUDS 调用 Soap API
【发布时间】:2016-09-01 05:45:25
【问题描述】:

我有一个肥皂 api,我想用 python 中的 suds 调用它。我已经安装了 suds 并且能够连接到 soap api。

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

但要注意的是,我必须将一些标头和额外的标头发送到soap api,而且我还必须在 TLSv1 中发送 ssl 文件。

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

但不知何故我无法做到这一点。 有人可以帮我如何从 suds 中调用soap api并在其中传递标题以及 ssl。谢谢你

【问题讨论】:

    标签: python-2.7 ssl soap suds


    【解决方案1】:

    我找到了解决方案。我已经在 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
    

    在这里,我还必须使用标题来发送肥皂。所以我也必须传递标题(可选)。

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

    我必须使用 ast,因为您必须以 json 格式发送标头。然后我调用这个函数。

    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中的函数。

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

    【讨论】:

      【解决方案2】:
      #**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
      

      【讨论】:

      • 您能否详细说明您的代码的作用?
      • 它在python中使用给定的标头和xml参数调用SOAP服务并从响应中获取参数
      猜你喜欢
      • 1970-01-01
      • 2011-02-12
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多