我最近遇到了同样的问题,不幸的是,suds 和 jurko-suds(一个维护的 suds 分支)都无法提供帮助。这主要是因为 suds 不断生成格式错误的肥皂信封(如果应该生成的肥皂有一些应该在 CDATA 中的内容,则尤其会发生这种情况)与服务器所期望的不同。即使我尝试使用 __inject 选项自己注入肥皂信封,情况也是如此。
这是我使用python请求解决它的方法
import requests
request = u"""<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://SOME_URL">
<soapenv:Header>
<user>{0}</user>
<pass>{1}</pass>
</soapenv:Header>
<soapenv:Body>
<req:RequestInfo><![CDATA[<?xml version='1.0' encoding='UTF-8'?><request xmlns='http://SOME_OTHER_URL'>
<Call>
<callID>{2}</callID>
...
</Call>
<Foo>
<Bar>
<Baz>{3}</Baz>
...
</Bar>
<Neytri>
...
</Neytri>
</Foo>
<Title>{4}</Title>
</request>]]></req:RequestInfo>
</soapenv:Body>
</soapenv:Envelope>""".format('Jake_Sully',
'super_secret_pandora_password',
'TYSGW-Wwhw',
'something_cool',
'SalaryPayment',
'Pandora_title',
)
encoded_request = request.encode('utf-8')
headers = {"Host": "http://SOME_URL",
"Content-Type": "application/soap+xml; charset=UTF-8",
"Content-Length": str(len(encoded_request)),
"SOAPAction": "http://SOME_OTHER_URL"}
response = requests.post(url="http://SOME_OTHER_URL",
headers = headers,
data = encoded_request,
verify=False)
print response.content #print response.text
真正重要的是在标头中指定 Content-Type 以及 SOAPAction。根据SOAP 1.1 规范
The SOAPAction HTTP request header field can be used to indicate the intent of the SOAP HTTP request. The value is a URI identifying the intent. SOAP places no restrictions on the format or specificity of the URI or that it is resolvable. An HTTP client MUST use this header field when issuing a SOAP HTTP Request.
SOAPAction 的值通常可以在您要进行的 API 调用的 wsdl 文件中找到;如果 wsdl 文件中不存在,则可以使用空字符串作为该标头的值
另见:
- http://archive.oreilly.com/pub/post/unraveling_the_mystery_of_soap.html
- http://www.prodigyproductionsllc.com/articles/programming/call-a-webservice-using-soap-and-python/