簡體   English   中英

如何使用suds向SOAP信封部分添加屬性?

[英]How to add attributes to SOAP envelope part, using suds?

我有一些方法的WSDL服務器,每個方法都接受DataModelType。 之前我以這種方式使用它:

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)

而且效果很好。

它使用DataRequest自動創建XML:

<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(argument)給了我從DataModelType構建的python對象

現在,我發現標簽DataRequest具有必須為服務器設置的參數,以便接收正確的響應。 但是我應該如何設置這些Python的扔泡沫 ,不exstracting XML,解析它,然后通過HTTP傳輸發送?

如果通過http傳輸發送只是一種方式,該怎么辦? 我正在嘗試模擬return func(argument)里面發生的事情,但是即使它采用完全相同的參數,結果也不同。

suds中是否有接口來設置它自己關心的convert屬性? 如何解決這個任務?

我解決了 除了要討論示例以外,我們還需要使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消息的任何字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM