我正在尝试使用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()
它正在运行,但它只使用一个命名空间。
我可以在这一行中定义多个服务:
application = Application([HelloWorldService, OtherService1, OtherService2]
但是可以为每个服务定义不同的命名空间吗?这样的事情不起作用:
tns=['spyne.examples.hello', 'http://other.service1', 'http://other.service2']
答案 0 :(得分:2)
您可以使用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()