XML中的python suds错误

2024-09-27 17:46:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我花了几个小时从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()

这是我发送的:

^{pr2}$

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

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

提前谢谢


Tags: 代码fromimportclienturlresponseservicesuds
1条回答
网友
1楼 · 发布于 2024-09-27 17:46:15

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

在下面的示例中,<SOAP-ENV:Envelope>标记被修改以符合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()])

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

相关问题 更多 >

    热门问题