python - 如何使用zeep为签名添加时间戳?

标签 python soap wsdl wsse zeep

我正在尝试创建一个使用 WSDL 文件并生成正确的 SOAP 消息的客户端。 这就是我创建客户端的方式:

client = Client(
        wsdl=wsdl
        ,transport = transport
        ,wsse = Signature('key.pem', 'cert.pem')
    )

zeep 代码中的注释表示它应该生成类似于以下内容的 XML:

  <soap:Header>
    <wsse:Security mustUnderstand="true">
      <wsu:Timestamp>
        <wsu:Created>2015-06-25T21:53:25.246276+00:00</wsu:Created>
        <wsu:Expires>2015-06-25T21:58:25.246276+00:00</wsu:Expires>
      </wsu:Timestamp>
    </wsse:Security>
  </soap:Header>

但是它没有添加 mustUnderstand 属性,并且 TimeStamp 为空。有谁知道如何确保这些字段设置正确?

最佳答案


    from datetime import datetime, timedelta
    from lxml import etree
    from zeep import Client
    from zeep.wsse import utils
    from zeep.plugins import HistoryPlugin

    # Справочники
    wsdl = 'http://claim-test2.isb.az:8903/cib/svc/wsdl/codetable.wsdl'
    username, password = 'ws', '********'
    bussines_user = '********'


    class UsernameTokenTimestamp:
        def __init__(self, username, password=None):
            self.username = username
            self.password = password

        def apply(self, envelope, headers):
            security = utils.get_security_header(envelope)

            created = datetime.now()
            expired = created + timedelta(seconds=5 * 60)

            token = utils.WSSE.UsernameToken()
            token.extend([
                utils.WSSE.Username(self.username),
                utils.WSSE.Password(self.password),
                utils.WSSE.Nonce('43d74dda16a061874d9ff27f2b40e017'),
                utils.WSSE.Created(utils.get_timestamp(created)),
            ])

            timestamp = utils.WSU('Timestamp')
            timestamp.append(utils.WSU('Created', utils.get_timestamp(created)))
            timestamp.append(utils.WSU('Expires', utils.get_timestamp(expired)))

            security.append(timestamp)
            security.append(token)

            #  
            # headers['Content-Type'] = 'application/soap+xml;charset=UTF-8'

            return envelope, headers

        def verify(self, envelope):
            pass


    history = HistoryPlugin()
    client = Client(
        wsdl=wsdl,
        wsse=UsernameTokenTimestamp(username=username, password=password),
        plugins=[history]
    )

关于python - 如何使用zeep为签名添加时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44642460/

相关文章:

java - 具有重复变量的 wsimport 和 WSDL

php - PHP 中的 Perfect Soap (Wsdl) Web 服务

python - 如何使用 Melt 将多个列名称作为 val_vars 传递?

python - "~"除了作为按位否定操作之外,在 python 中还能做什么?

php - 解析 SOAP 负载的 XML 错误 : Reserved XML Name

java - 如何从 SOAP 端点接收 xml 响应?

java - 在 WebSphere 8.5 上创建 webservice jax-ws 的问题

haskell - Haskell 中的 SOAP Web 服务?

python - 在其方法之外引用一个类?

Python Pygame 字体 x 坐标