简体   繁体   English

如何在控制台上打印“ hello world”

[英]How to print “hello world” to console

I have a SOAP server set up that receives SOAP messages from a foreign server. 我有一个设置为从外部服务器接收SOAP消息的SOAP服务器。 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. 我知道这不是Python语言,最好的方法是设置一个与服务器WSDL匹配的WSDL,但这超出了我的编程能力,目前还没有一个WSDL解析器库可供我使用。

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. 我想将此消息转换为字符串,然后将其转换为对象实例,其中的项目将具有UCPTname,UCPTlastUpdate,UCPTvalue等属性。

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>

You should install SoapPy (not Soapy, be careful) and take a look of the possible types that SOAP defines ( here ). 您应该安装SoapPy (请注意,不要安装Soapy),并查看SOAP定义的可能类型( 此处 )。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM