微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Python 中使用 Spyne 从两个不同元素读取具有相同名称的 Xml 属性

如何解决如何在 Python 中使用 Spyne 从两个不同元素读取具有相同名称的 Xml 属性

我是 Python 新手。我正在尝试使用 Python(版本 3.9.6)和 spyne-2.13.16 的第一个 SOAP 服务。在此服务中,我所做的只是读取 xml 负载并在 Json 中将其打印为输出

我不确定如何从具有相同名称的 SOAP 请求中的两个不同元素读取 xml 属性。我在下面附上了我的请求负载和响应负载

我有两个元素“generation_time”和“expiration_time”。它们都具有相同的属性“时间戳”。我在输出中看到代码打印了 expire_time 的时间戳属性的值。有没有办法可以控制需要打印哪个?

这是我的项目结构:

mypythonWS | |- 应用程序(模块) |- app.py |- 模型.py |

代码

app.py

​​>
from spyne import Application,rpc,ServiceBase
from spyne.protocol.json import JsonDocument
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication
import model

class MyPythonService(ServiceBase):
    @rpc(model.Root,_returns=model.Root)
    def getConfiguration(ctx,root):
        return root

application = Application([MyPythonService],'mypython.type.service',in_protocol=Soap11(),out_protocol=JsonDocument())

wsgi_app = WsgiApplication(application)

if __name__ == '__main__':
    import logging
    from wsgiref.simple_server import make_server

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

    logging.info("listening to http://127.0.0.1:8000")
    logging.info("wsdl is at: http://localhost:8000/?wsdl")

    server = make_server('0.0.0.0',8000,wsgi_app)
    server.serve_forever()

model.py

​​>
class MetaType(ComplexModel):
    __type_name__ = 'MetaType'
    _type_info = {
        'generation_time': Unicode,'microtime': XmlAttribute(Unicode),'timestamp': XmlAttribute(Unicode),'expiration_time': Unicode
    }
    
class Root (ComplexModel):
    __type_name__ = 'root'
    _type_info = {
        'Meta': MetaType
    }

请求:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <getConfiguration>
            <root>
                <Meta>
                    <generation_time microtime="1623853142.0784710" timestamp="1623853142">06/16/2021 2:19:02 PM UTC</generation_time>
                    <expiration_time timestamp="1635085143">10/24/2021 2:19:02 PM UTC</expiration_time>
                </Meta>
            </root>
        </getConfiguration>
    </soap:Body>
</soap:Envelope>

回复

{ 
    "Meta": {
        "generation_time": "06/16/2021 2:19:02 PM UTC","microtime": "1623853142.0784710","timestamp": "1635085143","expiration_time": "10/24/2021 2:19:02 PM UTC"
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。