Question

I have a SOAP server set up that receives SOAP messages from a foreign server. I want to be able to process these messages. Thanks to these two lines of code in my server, I am able to see the messages in my console:

logging.basicConfig(level=logging.INFO)
logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)

Is there anyway I can convert this message to a string and then to an object instance? I understand this isn't very pythonic and the best way would be to set up a WSDL that matches the server WSDL but this is beyond my programming capabilities, and at the moment there isn't a WSDL parser library I can use either.

This a sample message that I want to handle from the foreign server displayed in my console.

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

I want to convert this message to a string and then possibly an object instance where item would have attributes UCPTname, UCPTlastUpdate, UCPTvalue etc.

Please respond.

This is the full message:

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>

Was it helpful?

Solution

You should install SoapPy (not Soapy, be careful) and take a look of the possible types that SOAP defines (here).

Then you can parse your message like this:

#!/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'))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top