对于我手头的任务,我找到了部分问题的解决方案 - 我正在使用suds.plugin.MessagePlugin
实现来清除保存的 WebService 响应有效负载并防止 suds 将其解析为 DOM。这是我原来问题中的第 2 项:
避免运行 suds.sax.parser 并且不要构建包含整个响应树的 DOM 对象
下面是将此方法与公共 Web 服务一起使用的示例。
查看gist中的完整代码
import suds
class PayloadInterceptor(suds.plugin.MessagePlugin):
def __init__(self, *args, **kwargs):
self.last_payload = None
def received(self, context):
#recieved xml as a string
print "%s bytes received" % len(context.reply)
self.last_payload = context.reply
#clean up reply to prevent parsing
context.reply = ""
return context
if __name__=='__main__':
wsurl = "http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL"
payload_interceptor = PayloadInterceptor()
client = suds.client.Client(wsurl, plugins=[payload_interceptor])
print client
res = client.service.CapitalCity("NL")
print "received %s bytes" % len(payload_interceptor.last_payload)
print "parsed result: %s" % res
print "response payload: %s" % payload_interceptor.last_payload
产生输出:
...
received 336 bytes
parsed result: None
response payload: <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<m:CapitalCityResponse xmlns:m="http://www.oorsprong.org/websamples.countryinfo">
<m:CapitalCityResult>Amsterdam</m:CapitalCityResult>
</m:CapitalCityResponse>
</soap:Body>
</soap:Envelope>