python suds XML中的错误

时间:2013-04-25 14:41:38

标签: python soap suds

我尝试了几个小时从Soap Webservice接收数据。

这是我的代码:

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>

这就是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>

如何更新我的代码以发送有效请求?

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以实现一个Suds插件,在发送之前修改XML。

在下面的示例中,修改了<SOAP-ENV:Envelope>标记以匹配网络服务器的期望:

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()])

您必须完成marshalled方法才能完全转换XML。但在此之前,请检查您是否拥有正确的WSDL文件,如@Martijn Pieters所说。