python-2.7 - 使用 SSL 和 header 在 python 中通过 SUDS 调用 Soap API

标签 python-2.7 ssl soap suds

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

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

但要注意的是我必须向 soap api 发送一些 header 和额外的 header ,而且我还必须以 TLSv1 发送 ssl 文件。

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

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

最佳答案

我找到了解决方案。我在 pythonsuds 中完成了所有这些事情。请安装 suds。

所以首先我用 python 编写了一个 tranport.py 脚本,它实际上获取证书和 key 文件并将其传递给 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

在这里,我还必须使用 header 来发送 SOAP 。所以我还必须传递 header (可选)。

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

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

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>'

现在点击 SOAP 功能

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

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

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

关于python-2.7 - 使用 SSL 和 header 在 python 中通过 SUDS 调用 Soap API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39263532/

相关文章:

python - 反转数据框中的行值

javascript - 在 python 中使用 json.loads() 的问题

perl - 如何在 Perl 中将 SSL 选项传递给 SOAP::Lite

java - 各种风格的 Web 服务在性能上是否存在差异?

python - 将字典转换为 JSON

python - 从 "beginning to end" "over and over again"制作一个 side_effect 迭代器循环

docker - 通过 microk8s 获取 "x509: certificate signed by unknown authority"

python - SSLError : unknown error (_ssl. c :2825). 在 python 中验证 .cer 文件时

php - 如何使用流 stream_socket_client() 在 aruba 中打开 ssl 连接

php - 为什么 getLastRequest 不返回任何东西?