【问题标题】:python suds Error in XMLXML中的python suds错误
【发布时间】:2013-04-25 14:41:37
【问题描述】:

我尝试了几个小时从 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 = ''
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>

这正是网络服务器所期望的:

<?xml version="1.0" encoding="UTF-8"?>
<GATEWAY xmlns="urn:Software-com:Gateway:v7-00">
<CustomerService>
    <checkIfExists>
        <SessionID/>
        <UserID>test</UserID>
    </checkIfExists>
</CustomerService>
</GATEWAY>

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

提前致谢

【问题讨论】:

  • 您的网络服务器期望的示例肯定不是有效的 SOAP 请求; SUDS 只能发送 WSDL 指定的内容;如果 SUDS 生成的内容有误,则服务器向您发送了错误的 WSDL。
  • IIRC,在您在 url 上初始化客户端后执行print client它将显示 suds 为您提供的 Web 服务界面。它可能与您的想法略有不同。我记得我必须这样做才能让它正常工作一次:suds 只等待两个叶子对象,我在其中提供一个包含两个叶子对象的对象作为输入。

标签: python soap suds


【解决方案1】:

您可以实现一个 Suds 插件以在发送 XML 之前对其进行修改。

在下面的示例中,&lt;SOAP-ENV:Envelope&gt; 标签被修改以匹配网络服务器的期望:

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 所说。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多