kra*_*r65 6 python soap wsdl web-services
我收到了一些用于调用SOAP服务的示例php代码,我现在需要将其转换为Python.在PHP代码中,他们设置标题如下:
$auth = array();
$auth['token'] = 'xxx';
if ($auth) {
// add auth header
$this->clients[$module]->__setSoapHeaders(
new SoapHeader(
$namespace,
'auth',
$auth
)
);
}
Run Code Online (Sandbox Code Playgroud)
所以auth标题应如下所示:['token' => 'xxx'].然后我将wsdl加载到SoapUI中,它给了我以下示例xml:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sub="https://example.com/path/to/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>
Run Code Online (Sandbox Code Playgroud)
在pysimplesoap中我现在尝试这样的事情:
from pysimplesoap.client import SoapClient
WSDL = 'https://example.com/some/path/sub.wsdl'
TOKEN = 'xxx'
client = SoapClient(wsdl=WSDL, trace=True)
client['auth'] = {'token': TOKEN}
print client.customer_logos_pull({})
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误说ExpatError: not well-formed (invalid token): line 1, column 0,这是有道理的,因为在记录的xml中我看到标题是空的:
<soap:Header/>
Run Code Online (Sandbox Code Playgroud)
我尝试通过包含sub:之前的代码改变代码auth:client['sub:auth'] = {'token': TOKEN},但我得到了同样的错误.
有谁知道我在这里做错了什么?欢迎所有提示!
所以我认为我们可以通过使用suds库来解决这个问题。
以下是如何发送包含标头的 SOAP 请求的非常基本的示例:
例子:
from suds.sax.element import Element
client = client(url)
ssnns = ('ssn', 'http://namespaces/sessionid')
ssn = Element('SessionID', ns=ssnns).setText('123')
client.set_options(soapheaders=ssn)
result = client.service.addPerson(person)
Run Code Online (Sandbox Code Playgroud)
这是如何发送以下标头的示例:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP ENC="http://www.w3.org/2003/05/soap-encoding">
<ssn:SessionID SOAP-ENV:mustUnderstand="true">123</ssn:SessionID>
</SOAP-ENV:Header>
Run Code Online (Sandbox Code Playgroud)
注意:我实际上并没有尝试过这个,因为我无法访问任何可以测试的现成 SOAP/XML 服务!
因此,在您的特定示例中,您将执行以下操作:
>>> from suds.sax.element import Element
>>> subns = ("sub", "http://namespaces/sub")
>>> sub = Element("auth", ns=subns)
>>> token = Element("token").setText("?")
>>> user_id = Element("user_id").setText("?")
>>> user_token = Element("user_token").setText("?")
>>> sub.append(token)
Element (prefix=sub, name=auth)
>>> sub.append(user_id)
Element (prefix=sub, name=auth)
>>> sub.append(user_token)
Element (prefix=sub, name=auth)
>>> print(sub.str())
<sub:auth xmlns:sub="http://namespaces/sub">
<token>?</token>
<user_id>?</user_id>
<user_token>?</user_token>
</sub:auth>
Run Code Online (Sandbox Code Playgroud)
然后调用set_options()你的client对象:
client.set_options(soapheaders=sub)
Run Code Online (Sandbox Code Playgroud)
$ pip install suds
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
813 次 |
| 最近记录: |