Pregunta

I am using ws4py / CherryPy for websockets support, and would like to implement WAMP on top of it.

I thought of using autobahn but it only seems to support Twisted and asyncio out of the box.
Is possible to use autobahn functionality to extend ws4py, or is there an alternative way?

¿Fue útil?

Solución 2

As you have already noted, Autobahn|Python supports running under either Twisted or asyncio. It includes a full-featured WebSocket implementation, and WAMP on top of that. So there is no need for ws4py, and we have no plans of porting the WAMP layer that Autobahn|Python includes to ws4py.

Twisted also supports running any WSGI compliant application. So in principle, you should be able to run CherryPy under Twisted. I have not tested that - I only tested (and regularily use) Flask on Twisted.

Otros consejos

@oberstet is right. This is a quick showcase on how to run a CherryPy app:

import cherrypy
from twisted.web.wsgi import WSGIResource
from twisted.internet import reactor

# Our CherryPy application
class Root(object):
    @cherrypy.expose
    def index(self):
        return "hello world"

# Create our WSGI app from the CherryPy application
# it will respond to the /blog path
wsgiapp = cherrypy.tree.mount(Root(), '/blog', {'/': {'tools.etags.on': True}})

# Configure the CherryPy's app server
# Disable the autoreload which won't play well 
cherrypy.config.update({'engine.autoreload.on': False})

# We will be using Twisted HTTP server so let's
# disable the CherryPy's HTTP server entirely
cherrypy.server.unsubscribe()

# If you'd rather use CherryPy's signal handler
# Uncomment the next line. I don't know how well this
# will play with Twisted however
#cherrypy.engine.signals.subscribe()

# Tie our app to Twisted
reactor.addSystemEventTrigger('after', 'startup', cherrypy.engine.start)
reactor.addSystemEventTrigger('before', 'shutdown', cherrypy.engine.exit)
resource = WSGIResource(reactor, reactor.getThreadPool(), wsgiapp)

Assuming you save this snippet into a module called: "cptw.py", you can now serve your CherryPy application as follow:

twistd -n web --wsgi  cptw.wsgiapp

This works with Twisted 13.2 and CherryPy 3.2.6+

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top