Giacomo Lacava
Giacomo Lacava

Reputation: 1823

How do I run a Spyne application from web.py?

I have a working web.py application, and a working Spyne application. I'd like to make web.py route requests to the spyne app when matching a certain url.

I tried with a wrapper as per web.py docs but no luck.

in myspyne.py:

import logging
logging.basicConfig(level=logging.DEBUG)
from spyne.application import Application
from spyne.decorator import srpc
from spyne.service import ServiceBase
from spyne.model.primitive import Integer
from spyne.model.primitive import Unicode
from spyne.model.complex import Iterable
from spyne.protocol.soap import Soap11

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='my.custom.ns',
                      in_protocol=Soap11(validator='lxml'),
                      out_protocol=Soap11())

in myweb.py:

urls = (
    '/', 'index',
    '/myspyne/(.*)', myspyne.application, # this does not work
)

class index:
    def GET(self):
        return "hello"

app = web.application(urls, globals(), autoreload=False)
application = app.wsgifunc()
if __name__ == '__main__':
    app.run()

Upvotes: 0

Views: 518

Answers (1)

Burak Arslan
Burak Arslan

Reputation: 8001

You need to implement a web.py transport, or find a way to expose wsgi applications from web.py. That documentation you linked to is VERY old (seems decades ago to me :)).

I have no experience with web.py at all. But basing on the web.py part of that document, this could work:

def start_response(status, headers):
    web.ctx.status = status
    for header, value in headers:
        web.header(header, value)


class WebPyTransport(WsgiApplication):
    """Class for web.py """
    def GET(self):
        response = self(web.ctx.environ, start_response)
        return render("\n".join(response))

    def POST(self):
        response = self(web.ctx.environ, start_response)
        return render("\n".join(response))

With this, you can use:

application = Application(...)
webpy_app = WebPyTransport(application)

So urls becomes:

urls = (
    '/', 'index',
    '/myspyne/(.*)', myspyne.webpy_app,
)

I hope that helps.

Upvotes: 1

Related Questions