pythonsoapspyne

Multiple namespaces in spyne SOAP web service


I'm trying to host SOAP service in python3 using Spyne framework and this example part of code:

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()

It's working but it uses only one namespace, tns='spyne.examples.hello'.

I can define multiple services in this line:

application = Application([HelloWorldService, OtherService1, OtherService2]

But is it possible to define diffrent namespace for each service? Something like this doesn't work:

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

Solution

  • You can use WsgiMounter class for this.

    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,
    })
    

    Then, pass the wsgi_mounter object instead of wsgi_app in your code:

    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()