【问题标题】:Create xsd.Element with attribute and string value using Zeep使用 Zeep 创建具有属性和字符串值的 xsd.Element
【发布时间】:2021-09-11 17:18:00
【问题描述】:

我正在尝试使用 Zeep 创建一个自定义 SOAP 标头元素,该元素具有属性和字符串值。没有其他子元素。我确定我错过了一些关于如何制作它的信息,希望有人能指出我正确的方向。

我想要的是以下内容:

<wsse:Password Type="StringAttribute">StringPasswordValue</wsse:Password>

到目前为止我已经尝试过什么(我只是在这里猜测语法):

password_header = xsd.Element('{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}Password', xsd.ComplexType([
    xsd.String(),
    xsd.Attribute('Type', xsd.String())
    ]))

password_value = password_header('StringPasswordValue', Type='StringAttribute')

我也可能期望它在没有xsd.ComplexType 的情况下是可行的,只需指定xsd.String 的属性,但没有这样的运气。我不想使用wsse:UsernameToken 来做到这一点。

【问题讨论】:

    标签: python python-3.x zeep


    【解决方案1】:

    您可以使用zeep.wsse.utils 模块创建密码元素:

    from zeep.wsse.utils import WSSE
    password = WSSE.Password("StringPasswordValue", Type="StringAttribute")
    

    这是基于how the password is created for UsernameToken

    或者您可以直接使用lxml 创建元素(就像zeep does it)。

    from lxml.builder import ElementMaker
    
    wsse_ns = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    builder = ElementMaker(namespace=wsse_ns, nsmap={"wsse": wsse_ns})
    password = builder.Password("StringPasswordValue", Type="StringAttribute")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 2014-08-31
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      相关资源
      最近更新 更多