gpt4 book ai didi

python - spyne SOAP Web 服务中的多个命名空间

转载 作者:行者123 更新时间:2023-12-01 03:56:13 36 4
gpt4 key购买 nike

我正在尝试使用 Spyne 框架和此示例代码部分在 python3 中托管 SOAP 服务:

class HelloWorldService(ServiceBase):
@srpc(Unicode, Integer, _returns=Iterable(Unicode))
def say_hello(name, times):
for i in range(times):
yield 'Hello, %s' % name
application = Application([HelloWorldService],
tns='spyne.examples.hello',
in_protocol=Soap11(),
out_protocol=Soap11()
)
if __name__ == '__main__':
# You can use any Wsgi server. Here, we chose
# Python's built-in wsgi server but you're not
# supposed to use it in production.
from wsgiref.simple_server import make_server
wsgi_app = WsgiApplication(application)
server = make_server('0.0.0.0', 8000, wsgi_app)
server.serve_forever()

它可以工作,但仅使用一个命名空间,tns='spyne.examples.hello'

我可以在这一行定义多个服务:

application = Application([HelloWorldService, OtherService1, OtherService2]

但是是否可以为每个服务定义不同的命名空间?像这样的东西不起作用:

tns=['spyne.examples.hello', 'http://other.service1', 'http://other.service2']

最佳答案

您可以使用 WsgiMounter 类来实现此目的。

from spyne.util.wsgi_wrapper import WsgiMounter

app1 = Application([SomeService], tns=namespace1,
in_protocol=Soap11(), out_protocol=Soap11())
app2 = Application([SomeOtherService], tns=namespace2,
in_protocol=Soap11(), out_protocol=Soap11())
wsgi_mounter = WsgiMounter({
'app1': app1,
'app2': app2,
})

然后,在代码中传递 wsgi_mounter 对象而不是 wsgi_app:

if __name__ == '__main__':
from wsgiref.simple_server import make_server
wsgi_mounter = WsgiMounter({
'app1': app1,
'app2': app2,
})
server = make_server('0.0.0.0', 8000, wsgi_mounter)
server.serve_forever()

关于python - spyne SOAP Web 服务中的多个命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37419884/

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