2013-04-25 92 views
1

錯誤我嘗試了幾個小時從Soap Webservice接收數據。python suds XML

這是我的代碼:

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 = '[email protected]' 
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>[email protected]</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> 

我如何更新我的代碼發送有效的請求?

在此先感謝

+0

你的web服務器期望的例子當然不是一個有效的SOAP請求; SUDS只能發送WSDL指定的內容;如果SUDS產生的是錯誤的,那麼服務器向你發送錯誤的WSDL。 – 2013-04-25 14:46:29

+0

IIRC,在你初始化URL上的客戶端之後做一個'打印客戶端',**它將顯示界面**,這個界面**是爲你提供的Web服務。它可能與您的想法略有不同。我記得我不得不這樣做,因爲它只能正常工作:僅等待兩個葉對象,在那裏我給出了一個包含兩個葉對象的對象。 – 2013-04-25 14:58:24

回答

0

您可以實現無泡沫插件在發送前修改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所說。