如何使用Python(suds)签署XML

2024-04-20 04:41:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试签署一个由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)

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

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

在肥皂水.WebFault:服务器引发错误:“处理标头时发现错误”


Tags: fromimportselfclienturlcertrequestreply
1条回答
网友
1楼 · 发布于 2024-04-20 04:41:17

我发现pythonwsse(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,
                ),
            ],
        )

相关问题 更多 >