【问题标题】:How to return an object with spyne如何用 spyne 返回一个对象
【发布时间】:2018-01-30 20:11:12
【问题描述】:

我需要从 spyne 服务器方法返回一个对象。我读到 ComplexModel 是可能的,但这实际上是一个空的结果。我应该怎么做才能让它正常工作?

这是我的代码:

class Bndbox(ComplexModel):
    xmin = 0
    ymin = 0
    xmax = 0    
    ymax = 0
    def __init__(self, xmin, ymin, xmax, ymax):
        self.xmin = xmin
        self.ymin = ymin
        self.xmax = xmax
        self.ymax = ymax

class TestService(Service):
    @srpc(Unicode, _returns=Bndbox)
    def service_method(encoded_string):
        print(encoded_string)
        myBndbox = Bndbox(10, 20, 30, 40)

        print(myBndbox.xmin)
        print(myBndbox.xmax)
        print(myBndbox.ymin)
        print(myBndbox.ymax)

        return myBndbox

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

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

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

    application = Application([TestService], tns='test_service', in_protocol=Soap11(validator='lxml'), out_protocol=Soap11())
    wsgi_application = WsgiApplication(application)

    server = make_server('localhost', 8080, wsgi_application)
    server.serve_forever()

【问题讨论】:

    标签: python spyne


    【解决方案1】:

    就 spyne 而言,您的对象是空的。你可以这样修复它:

    from spyne import Integer64
    
    class Bndbox(ComplexModel):
        xmin = Integer64
        ymin = Integer64
        xmax = Integer64
        ymax = Integer64
    
        def __init__(self, xmin, ymin, xmax, ymax):
            # don't forget to call parent class initializer
            super(BndBox, self).__init__()
    
            self.xmin = xmin
            self.ymin = ymin
            self.xmax = xmax
            self.ymax = ymax
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 2015-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多