无效的SUDS封包
当我尝试执行这段代码时:
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创建一个<SOAP-ENV:Body>
的标签,而不是<ns1:Body>
?
我试着看看能不能改变发送的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元素的前缀改成了服务器要求的格式。太开心了!
1 个回答
3
很遗憾,这个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()])