我有一个SOAP服务器设置,它接收来自外部服务器的SOAP消息。我希望能够处理这些消息。感谢我服务器中的这两行代码,我能够在控制台中看到这些消息:
logging.basicConfig(level=logging.INFO)
logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)
无论如何我可以将此消息转换为字符串然后转换为对象实例吗? 我知道这不是pythonic,最好的方法是设置一个与服务器WSDL匹配的WSDL,但这超出了我的编程能力,目前还没有WSDL解析器库,我可以使用无论是。
这是我想从控制台中显示的外部服务器处理的示例消息。
<Item xsi:type="Dp_Data">
<UCPTname>Net/MB485/MAIN POWER/Fb/PowerSum</UCPTname>
<UCPTlastUpdate>2014-04-04T13:34:17.441-04:00</UCPTlastUpdate>
<UCPTformatDescription>#0000000000000000[0].SNVT_power_f#SI</UCPTformatDescription>
<UCPTvalue LonFormat="#0000000000000000[0].SNVT_power_f#SI">9330.949</UCPTvalue>
<UCPTpointStatus>AL_OFFLINE</UCPTpointStatus>
<UCPTpriority>255</UCPTpriority>
<UCPTpropagate>0</UCPTpropagate>
</Item>
我想将此消息转换为字符串,然后可能是一个对象实例,其中item将具有属性UCPTname,UCPTlastUpdate,UCPTvalue等。
请回复。
这是完整的信息:
DEBUG:spyne.protocol.xml:[1;32mMethod request string:[0m {http://wsdl.echelon.com/web_services_ns/ilon100/v4.0/message/}Write
DEBUG:spyne.protocol.xml:<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:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
<p:messageProperties xmlns:p="http://wsdl.echelon.com/web_services_ns/ilon100/v4.0/message/">
<p:UCPTtimeStamp>2014-04-04T15:38:03.641-04:00</p:UCPTtimeStamp>
<p:UCPTuniqueId>03000029B512</p:UCPTuniqueId>
<p:UCPTipAddress>10.217.247.36</p:UCPTipAddress>
<p:UCPTport>80</p:UCPTport>
<p:UCPTlastUpdate>2014-04-04T19:38:03Z</p:UCPTlastUpdate>
</p:messageProperties>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<Write xmlns="http://wsdl.echelon.com/web_services_ns/ilon100/v4.0/message/">
<iLonItem><Item xsi:type="Dp_Data">
<UCPTname>Net/MB485/MAIN POWER/Fb/PowerSum</UCPTname>
<UCPTlastUpdate>2014-04-04T15:37:33.891-04:00</UCPTlastUpdate>
<UCPTformatDescription>#0000000000000000[0].SNVT_power_f#SI</UCPTformatDescription>
<UCPTvalue LonFormat="#0000000000000000[0].SNVT_power_f#SI">95860.3</UCPTvalue>
<UCPTpointStatus>AL_NO_CONDITION</UCPTpointStatus>
<UCPTpriority>255</UCPTpriority>
<UCPTpropagate>1</UCPTpropagate>
</Item>
</iLonItem>
</Write>
</SOAP-ENV:Body>
答案 0 :(得分:1)
您应该安装SoapPy(不要Soapy,请注意)并查看SOAP定义的可能类型(here)。
然后你可以像这样解析你的消息:
#!/usr/bin/env python
# http://stackoverflow.com/questions/22869427/how-to-print-hello-world-to-console
import SOAPpy
import logging
logging.getLogger().setLevel(logging.DEBUG)
item_str='<?xml version="1.0" encoding="utf-8"?>\
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" \
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" \
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">\
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">\
<Item xsi:type="Dp_Data">\
<UCPTname>Net/MB485/MAIN POWER/Fb/PowerSum</UCPTname>\
<UCPTlastUpdate>2014-04-04T13:34:17.441-04:00</UCPTlastUpdate>\
<UCPTformatDescription>#0000000000000000[0].SNVT_power_f#SI</UCPTformatDescription>\
<UCPTvalue LonFormat="#0000000000000000[0].SNVT_power_f#SI">9330.949</UCPTvalue>\
<UCPTpointStatus>AL_OFFLINE</UCPTpointStatus>\
<UCPTpriority>255</UCPTpriority>\
<UCPTpropagate>0</UCPTpropagate>\
</Item>\
</soap:Body>\
</soap:Envelope>'
expected = { 'Item': {
'UCPTname': (SOAPpy.NS.XSD, "string"),
'UCPTlastUpdate': (SOAPpy.NS.XSD, "dateTime"),
'UCPTpriority': (SOAPpy.NS.XSD, "unsignedByte"),
'UCPTpropagate': (SOAPpy.NS.XSD, "boolean"),
}}
item = SOAPpy.parseSOAPRPC(item_str, rules=expected)
logging.debug("Name: %s" % item['UCPTname'])
logging.debug("Last update: %s" % (item['UCPTlastUpdate'],))
logging.debug("Priority: %s" % item['UCPTpriority'])
logging.debug("Should this be propagated? %s" % ('Yeep' if item['UCPTpropagate'] else 'Nopes'))