【问题标题】:How to sign XML with python ( suds)如何使用 python ( suds ) 签署 XML
【发布时间】:2016-10-31 18:13:18
【问题描述】:

我一直在尝试签署一个由 suds 创建的 XML 对象,但我没有运气。

我当前的脚本如下所示。

from suds.client import Client
from suds.transport.http import HttpAuthenticated
from suds.transport import Reply, TransportError

import requests

class RequestsTransport(HttpAuthenticated):

    def __init__(self, **kwargs):
        self.cert = kwargs.pop('cert', None)
        HttpAuthenticated.__init__(self, **kwargs)

    def send(self, request):
        self.addcredentials(request)
        resp = requests.post(
            request.url,
            data=request.message,
            headers=request.headers,
            cert=self.cert,
            verify=True
        )
        result = Reply(resp.status_code, resp.headers, resp.content)
        return result

url = 'URL'
headers = {"Content-Type": "text/xml;charset=UTF-8",
           "SOAPAction": ""}
t = RequestsTransport(cert=("path to cert","path to key"))
client = Client(url, headers=headers, transport=t)

我创建了一个方法,然后我需要对其进行签名。我有一个我正在检查的 WSDL 的公共证书的 pem 文件。

另外,如果我不签署请求,我会得到:

suds.WebFault:服务器引发错误:“处理标头时发现错误”

【问题讨论】:

    标签: python soap suds


    【解决方案1】:

    我发现 python-wsse (https://py-wsse.readthedocs.io/en/latest/) 可以像魅力一样处理泡沫。

        from suds.client import Client
        from suds.wsse import Security, Timestamp
        from wsse.suds import WssePlugin
    
        def get_client(our_keyfile_path, our_certfile_path, their_certfile_path):
            wsse = Security()
            wsse.tokens.append(Timestamp())
    
            return Client(
                wsdl_url,
                transport=transport,
                wsse=wsse,
                plugins=[
                    WssePlugin(
                        keyfile=our_keyfile_path,
                        certfile=our_certfile_path,
                        their_certfile=their_certfile_path,
                    ),
                ],
            )
    

    【讨论】:

    • transport 来自哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多