如何用spyne返回一个对象

时间:2018-01-30 20:11:12

标签: python spyne

我需要从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()

1 个答案:

答案 0 :(得分: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