【问题标题】:invalid SUDS envelope无效的 SUDS 信封
【发布时间】:2011-01-05 15:18:04
【问题描述】:

当我尝试执行这段代码时:

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>

所以我的问题是我可以强制 suds 将 body 标签创建为 &lt;SOAP-ENV:Body&gt; 而不是 &lt;ns1:Body&gt; 还是

我尝试了这个,看看我是否可以更改通过网络发送的 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 更改为使用 marshalled 而不是发送,这似乎成功了

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 元素的前缀以符合服务器的要求。快乐!

【问题讨论】:

  • 嗨 davideagle,我用你的帖子修改了我自己的标题。但是我将 suds 设置为 suds.client 的调试模式,我看到尽管上下文根据我的需要进行了更改。但 suds 仍在发送之前的内容。你能帮帮我吗?
  • 在再次运行 setup.py build 和 setup.py install 之前,您是否删除了之前的 egg 文件?
  • 为什么我需要这样做?我正在使用virtualenv,然后是pip install suds
  • 我下载了源代码并对此处提到的 EnvelopeFixer 进行了更改,卸载了之前的 egg 文件并运行了 setup.py build && setup.py install。安装不会更新安装的 egg 文件,因为它与以前的版本相同,这就是我手动删除它的原因

标签: python soap suds


【解决方案1】:

很遗憾,RPC 端点没有正确解析 XML。一种解决方法是向您的Client() 添加一个插件,在发送前编辑信封。 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()])

【讨论】:

  • 在发送方法中,上下文似乎不是文本,而是 suds.plugin.MessageContext 类型,所以我至少不能直接访问 xml 字符串
  • 我正在使用 suds 0.4.0。插件在这里调用:fedorahosted.org/suds/browser/trunk/suds/client.py#L635
  • 我试了一下并使用wireshark查看实际消息,但事实证明,当我执行context.envelope = context.envelope.upper()时,它不会改变通过网络发送的消息
  • 谢谢,这让我走上了正轨,我使用编组方法解决了这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-10
  • 1970-01-01
  • 1970-01-01
  • 2021-05-28
相关资源
最近更新 更多