XML中的python suds错误
我花了几个小时从Soap Web服务接收数据 这是我的代码:XML中的python suds错误,python,soap,suds,Python,Soap,Suds,我花了几个小时从Soap Web服务接收数据 这是我的代码: from suds.client import Client from suds import WebFault WSDL_URL = 'gatewaywebservice.asmx?wsdl' client = Client(WSDL_URL) checkIfExists = client.factory.create('checkIfExists') checkIfExists.SessionID = '' checkIfEx
from suds.client import Client
from suds import WebFault
WSDL_URL = 'gatewaywebservice.asmx?wsdl'
client = Client(WSDL_URL)
checkIfExists = client.factory.create('checkIfExists')
checkIfExists.SessionID = ''
checkIfExists.UserID = 'ttester@email.com'
try:
response = client.service.CustomerService(checkIfExists)
#print response
if response.Error:
print response.Error
else:
pass
except WebFault, e:
print e
print client.last_sent()
print client.last_received()
这是我发送的:
<SOAP-ENV:Envelope xmlns:ns0="asdf" xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<ns1:Body>
<ns0:CustomerService>
<ns0:CustomerService>
<ns0:SessionID></ns0:SessionID>
<ns0:UserID>ttester@email.com</ns0:UserID>
</ns0:CustomerService>
</ns0:CustomerService>
</ns1:Body>
</SOAP-ENV:Envelope>
ttester@email.com
这就是Web服务器所期望的:
<?xml version="1.0" encoding="UTF-8"?>
<GATEWAY xmlns="urn:Software-com:Gateway:v7-00">
<CustomerService>
<checkIfExists>
<SessionID/>
<UserID>test</UserID>
</checkIfExists>
</CustomerService>
</GATEWAY>
测试
如何更新代码以发送有效请求
提前感谢您可以实现一个Suds插件,在发送XML之前对其进行修改 在下面的示例中,
标记被修改以符合Web服务器的期望:
from suds.client import Client
from suds.plugin import MessagePlugin
class MyPlugin(MessagePlugin):
def marshalled(self, context):
envelope = context.envelope
envelope.name = 'GATEWAY'
envelope.setPrefix(None)
envelope.nsprefixes = {'xmlns' : 'urn:Software-com:Gateway:v7-00'}
# and so on...
# envelope[0] is the Header tag, envelope[1] the Body tag
# you can use "print context.envelope" to view the modified XML
client = Client(WSDL_URL, plugins=[MyPlugin()])
您必须完成
封送
方法才能完全转换XML。但在这样做之前,请检查您是否拥有正确的WSDL文件,如@Martijn Pieters所说。Web服务器所期望的示例肯定不是有效的SOAP请求;sud只能发送WSDL指定的内容;如果SUDS生成的是错误的,那么服务器向您发送了错误的WSDL.IIRC,在您在url上初始化客户端之后,执行打印客户端
,它将显示SUDS为web服务向您建议的接口。这可能与你的想法略有不同。我记得有一次我必须这样做,它才能正常工作:sud只等待两个leaf对象,其中我提供了一个包含这两个leaf对象的对象作为输入。