2011-01-05 53 views
2

當我嘗試執行此代碼:創建無效SUDS信封

mmurl = 'http://server/_mmwebext/mmwebext.dll?WSDL?server=localhost' 
mmclient = Client(mmurl, plugins=[EnvelopeFixer()]) 
loginResult = mmclient.service[1].Login(u'localhost', u'user', u'pass') 

以下信封:

<SOAP-ENV:Envelope xmlns:ns0="http://menandmice.com/webservices/" 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:Login> 
     <ns0:server>localhost</ns0:server> 
     <ns0:loginName>user</ns0:loginName> 
     <ns0:password>pass</ns0:password> 
     </ns0:Login> 
    </ns1:Body> 
</SOAP-ENV:Envelope> 

返回:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <SOAP-ENV:Body> 
     <SOAP-ENV:Fault> 
     <faultcode>SOAP-ENV:Client</faultcode> 
     <faultstring>Invalid command.</faultstring> 
     <detail> 
      <mmfaultdetails> 
       <message>Invalid command.</message> 
       <errorcode>5001</errorcode> 
      </mmfaultdetails> 
     </detail> 
     </SOAP-ENV:Fault> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

,但如果我改變它在soapUI至

<SOAP-ENV:Envelope xmlns:ns0="http://menandmice.com/webservices/" 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/> 
    <SOAP-ENV:Body> 
     <ns0:Login> 
     <ns0:server>localhost</ns0:server> 
     <ns0:loginName>user</ns0:loginName> 
     <ns0:password>pass</ns0:password> 
     </ns0:Login> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

它成功返回

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <SOAP-ENV:Body> 
     <LoginResponse xmlns="http://menandmice.com/webservices/"> 
     <session>UEk6A0UC5bRJ92MqeLiU</session> 
     </LoginResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

所以我的問題是我可以迫使泡沫創造body標籤作爲<SOAP-ENV:Body>代替<ns1:Body>或者是

我嘗試這樣做,看看是否我可以改變發送過來的XML線和使用wireshark嗅探流量,事實證明,它並沒有改變發送的消息。發送方法被稱爲是肯定的,因爲我看到打印出來的控制檯

class EnvelopeFixer(MessagePlugin): 
    def sending(self, context): 
     # context is the envelope text 
     print type(context) 
     context.envelope = context.envelope.upper() 
     print context.envelope 

     return context 

我改變EnvelopeFixer使用編組,而不是發送,這似乎已經完成了招

class EnvelopeFixer(MessagePlugin): 

    def marshalled(self, context): 
     root = context.envelope.getRoot() 
     envelope = root.getChild("Envelope") 
     envelope.getChildren()[1].setPrefix("SOAP-ENV") 
     print envelope.getChildren()[1] 

     return context 

所以現在我已經更改了body元素的前綴以符合服務器的要求。喜悅!

+0

嗨davideagle,我用你的文章來修改我自己的標題。但是我將suds設置爲suds.client的調試模式,我發現雖然上下文根據我的需要而改變。但是泡沫仍在發送之前的內容。你可以幫我嗎? – 2014-01-24 07:39:00

+0

在運行setup.py build和setup.py之前,您是否刪除了以前的egg文件? – davideagle 2014-01-24 13:59:57

+0

爲什麼我需要這樣做?我使用的是'virtualenv',然後是'pip安裝泡沫' – 2014-01-25 06:12:42

回答

3

令人遺憾的是RPC端點不能正確解析XML。一種解決方法是在發送前編輯信封的Client()中添加一個插件。 A MessagePlugin使您有機會在發送前修改suds.sax.document.Document或消息文本。

from suds.plugin import MessagePlugin 
class EnvelopeFixer(MessagePlugin): 
    def sending(self, context): 
     # context is the envelope text 
     context.envelope = context.envelope.upper() 
     return context 

client = Client(..., plugins=[EnvelopeFixer()]) 
+0

在發送方法上下文似乎不是文本,而是類型suds.plugin.MessageContext,所以我無法至少直接訪問xml字符串 – davideagle 2011-01-06 14:44:22

+0

我使用的是suds 0.4.0。這個插件在這裏被調用:https://fedorahosted.org/suds/browser/trunk/suds/client.py#L635 – joeforker 2011-01-06 15:15:12

+0

我給了這個鏡頭,並用wireshark查看實際的消息,但事實證明它並沒有改變當我做context.envelope = context.envelope.upper() – davideagle 2011-01-06 15:46:15