gpt4 book ai didi

python - 将命名空间添加到 Suds 中的默认 WSSE 安全对象

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:02 26 4
gpt4 key购买 nike

我了解如何向 SOAP 请求添加 header 。但这会生成一个与我需要传递的不匹配的 header 。返回此 header :

   <SOAP-ENV:Header>
<wsse:Security mustUnderstand="true">
<wsse:UsernameToken>
<wsse:Username>CABLE</wsse:Username>
<wsse:Password>CABLE</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</SOAP-ENV:Header>

但是,我需要修改该 header 的命名空间,以便为 Security 对象和 UsernameToken 对象传递特定的命名空间。我似乎无法弄清楚如何覆盖提供的默认值。

<soapenv:Header>
<wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
http://schemas.xmlsoap.org/ws/2002/07/secext
<wsse:UsernameToken xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility">
<wsse:Username>CABLE</wsse:Username>
<wsse:Password Type="wsse:PasswordText">CABLE</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>

这是生成上述内容的 Python 代码

security = Security()
token = UsernameToken('CABLE', 'CABLE')
security.tokens.append(token)
client.set_options(wsse=security)

最佳答案

想通了。这是答案。只需要使用 ns 参数

def createWSSecurityHeader(username,password):
# Namespaces
wsse = ('wsse', 'http://schemas.xmlsoap.org/ws/2002/07/secext')

# Create Security Element
security = Element('Security', ns=wsse)

# Create UsernameToken, Username/Pass Element
usernametoken = Element('UsernameToken', ns=wsse)
uname = Element('Username', ns=wsse).setText(username)
passwd = Element('Password', ns=wsse).setText(password)

# Add Username and Password elements to UsernameToken element
usernametoken.insert(uname)
usernametoken.insert(passwd)

security.insert(usernametoken)
return security

关于python - 将命名空间添加到 Suds 中的默认 WSSE 安全对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7168293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com