繁体   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