gpt4 book ai didi

python - 如何使用 Python suds 库创建 SOAP 头?

转载 作者:行者123 更新时间:2023-12-01 04:40:11 26 4
gpt4 key购买 nike

我需要使用如下消息调用 SOAP 服务:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sub="https://secure.xpslogic.com/service/wijnen/sub">
<soapenv:Header>
<sub:auth>
<token>?</token>
<!--Optional:-->
<user_id>?</user_id>
<!--Optional:-->
<user_token>?</user_token>
</sub:auth>
</soapenv:Header>
<soapenv:Body>
<sub:customer_logos_pull>
<!--Optional:-->
<language>?</language>
<!--Optional:-->
<limit>?</limit>
<!--Optional:-->
<options_utc>?</options_utc>
</sub:customer_logos_pull>
</soapenv:Body>
</soapenv:Envelope>

我有一些 php 示例代码,其设置 header 如下(并且效果很好):

auth = 数组();$auth[' token '] = 'xxx';如果($auth){ //添加授权头 $this->clients[$module]->__setSoapHeaders( 新的 SoapHeader( $命名空间, '授权', $授权 ) );}

我现在使用 Python suds lib 构造(空)主体和 header ,如下所示:

from suds.client import Client
from suds import WebFault

client = Client(url='https://example.com/sub.wsdl')

auth = client.factory.create('auth')
auth.token = 'xxx'
client.set_options(soapheaders=auth)

customerLogosPull = client.factory.create('customer_logos_pull')
result = client.service.customer_logos_pull(customerLogosPull)

但这给了我一个格式不正确(无效 token )消息。打开日志记录时,我发现这是消息:

DEBUG:suds.mx.core:processing:
(Content){
tag = "auth"
value =
(auth){
token = "xxx"
user_id = None
user_token = None
}
type = <Element:0x10ff8c950 name="auth">
<Complex:0x10ff8cbd0>
<Sequence:0x10ff8cc50>
<Element:0x10ff8cd10 name="token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
<Element:0x10ff8cd50 name="user_id" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
<Element:0x10ff8cd90 name="user_token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
</Sequence>
</Complex>
</Element>
}

它在我看来相当不错,但它也给出了一个格式不正确(无效 token )。看到 suds docs has 3 examples关于如何传递 SOAP 头,我也尝试了其他两个:

>>> token = client.factory.create('auth.token')
>>> token.set(TOKEN)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: token instance has no attribute 'set'

>>> client.set_options(soapheaders={'auth': {'token': 'xxx'}})
>>> customerLogosPull = client.factory.create('customer_logos_pull')
>>> result = client.service.customer_logos_pull(customerLogosPull)

它在日志中提供了此内容,但仍然是一个格式不正确(无效 token ):

(Content){
tag = "auth"
value =
{
token = "xxx"
}
type = <Element:0x106049290 name="auth">
<Complex:0x106049510>
<Sequence:0x106049590>
<Element:0x106049650 name="token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
<Element:0x106049690 name="user_id" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
<Element:0x1060496d0 name="user_token" type="(u'string', u'http://www.w3.org/2001/XMLSchema')" />
</Sequence>
</Complex>
</Element>
}

有人知道如何使用 Python 正确设置 header 中的 token 吗?欢迎所有提示!

最佳答案

我的 SOAP 头工作如下:

from suds.sax.element import Element
ssnp = Element("xsi:SessionHeader").append(Element('xsi:sessionId').setText("xxxxxxx"))
client.set_options(soapheaders=ssnp)

对应于下面soap请求xml中显示的soap header

<SOAP-ENV:Header>
<xsi:SessionHeader>
<xsi:sessionId>xxxxxxx</xsi:sessionId>
</xsi:SessionHeader>
</SOAP-ENV:Header>

我们可以使用print client.last_sent()查看发送了什么请求

关于python - 如何使用 Python suds 库创建 SOAP 头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30874988/

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