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

时间:2014-06-25 12:50:04

标签: python 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>

return func(argument)让我回复了从 DataModelType

构建的python对象

现在我发现标记 DataRequest 具有我必须为服务器设置以接收正确响应的参数。但是我应该如何设置pythonic throw suds ,而不是解析XML,解析它,然后通过http传输发送它?

如果通过http传输发送只是一种方式,该怎么办?我正在尝试模拟 return func(argument)内部的内容,但即使它采用完全相同的参数,结果也不同。

suds中是否有界面来设置自己关心的转换属性?如何解决这个问题?

1 个答案:

答案 0 :(得分: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消息的任何字段。

相关问题