2014-06-25 27 views
0

我有一些方法的WSDL服務器,每個方法接受DataModelType。 此前我用過這種方式:如何使用泡沫添加屬性到SOAP信封部分?

import suds.transport.http 
from suds.client import Client 

trans = suds.transport.http.HttpAuthenticated(username=username, 
               password=password) 
__client = Client(url, transport=trans) 
func = __client.service.__getattr__("ReceiveData") 
argument = __client.factory.create('DataModelType') 
argument["Key"] = "0001" 
return func(argument) 

它完美的工作。

它創建的XML與DataRequest自動:

<Envelope> 
    <Body> 
    <DataRequest> 
     <DataModelType> 
     <Key>0001</Key> 
     <Data></Data> 
     </DataModelType> 
    </DataRequest> 
    </Body> 
</Envelope> 

發送的,以及作爲迴應,我得到的東西,如:

<Envelope> 
    <Body> 
    <DataResponse> 
     <DataModelType> 
     <Key>0001</Key> 
     <Data>"Help me solve that magic, please"</Data> 
     </DataModelType> 
    </DataResponse> 
    </Body> 
</Envelope> 

而且回報FUNC(參數)給我回Python對象建from DataModelType

現在我發現標籤DataRequest有參數,我必須設置爲服務器以接收正確的響應。但是,我應該如何設置它們pythonic throw 泡沫,不提取XML,解析它,然後通過http傳輸發送它?

如果通過http傳輸發送只是一種方式,該怎麼做呢?我試圖模擬返回func(參數)裏面的內容,但即使它採用完全相同的參數,結果也是不同的。

泡沫中是否有界面來設置它自己關心的轉換屬性?如何解決這個任務?

回答

0

我解決了它。 而不是示例有問題,我們需要使用func更具體。

import suds.transport.http 
from suds.client import Client 

trans = suds.transport.http.HttpAuthenticated(username=username, 
               password=password) 
__client = Client(url, transport=trans) 
func = __client.service.__getattr__("ReceiveData") 
argument = __client.factory.create('DataModelType') 
argument["Key"] = "0001" 

#getting internal soap class and extracting entire soap_object: 
clientclass = func.clientclass({}) 
SoapClient = clientclass(func.client, func.method) 
binding = func.method.binding.input 
soap_xml = binding.get_message(func.method, [modelthings], {}) 

#fix anything what we want, that is representation of all envelope that should be sent   
soap_xml.children[0].children[1].children[0].attributes.append(u'attachmentInfo="true"') 
soap_xml.children[0].children[1].children[0].attributes.append(u'attachmentData="true"') 
soap_xml.children[0].children[1].children[0].attributes.append(u'ignoreEmptyElements="true"') 

#then prepare it as suds would do it: 
SoapClient.last_sent(soap_xml) 
plugins = PluginContainer(SoapClient.options.plugins) 
plugins.message.marshalled(envelope=soap_xml.root()) 
if SoapClient.options.prettyxml: 
    soap_xml = soap_xml.str() 
else: 
    soap_xml = soap_xml.plain() 
soap_xml = soap_xml.encode('utf-8') 
plugins.message.sending(envelope=soap_xml) 
request = Request(SoapClient.location(), soap_xml) 
request.headers = SoapClient.headers() 
my_reply = SoapClient.options.transport.send(request) 

ctx = plugins.message.received(reply=my_reply) 
my_reply = ctx.reply 
if SoapClient.options.retxml: 
    answer = my_reply 
else: 
    answer = SoapClient.succeeded(binding, my_reply) 

return answer 

因此,我們仍然儘可能使用suds作爲最大值,但可以接收對整個SOAP消息的任何字段的訪問。