3

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?

Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
mar10
  • 14,320
  • 5
  • 39
  • 64

2 Answers2

4

@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+

  • I vote this answer up, because its certainly useful to some people. But for us, CherryPy is set as base server (we instead run other WSGI apps on top of it), so I ended up re-implementing a WAMP-like protocol on top of CherryPy. – mar10 Apr 23 '14 at 13:32
  • Thanks. I'm sorry if my response came too late. I have a feeling WSGI is meant to enable cross-framework scenarios. – Sylvain Hellegouarch Apr 23 '14 at 14:54
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.

oberstet
  • 21,353
  • 10
  • 64
  • 97
  • I am tied to CherryPy, so I was hoping for a way to run autobahn|python as WSGI app inside CherryPy (as ws4py does) or to utilize some autobahn WAMP code on top of ws4py. Thank you you anyway. – mar10 Jan 26 '14 at 16:03
  • 1
    You might research if CherryPy (which is a WSGI compliant application itself, if I get this right), can run under Twisted running as a WSGI container. Here is how that works for Flask: https://github.com/tavendo/AutobahnPython/tree/master/examples/twisted/websocket/echo_wsgi – oberstet Jan 26 '14 at 16:26