gpt4 book ai didi

python - 如何使用spyne返回对象

转载 作者:行者123 更新时间:2023-12-01 02:13:50 25 4
gpt4 key购买 nike

我需要从 spy 服务器方法返回一个对象。我读到使用 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()

最佳答案

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

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

关于python - 如何使用spyne返回对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48529730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com