如何 return 使用 spyne 的对象

How to return an object with spyne

我需要 return 来自 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()

就 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