0

I am trying to implement websockets on a openshift.com server (which should support them).

openshift.com provides me a WSGI, so I embed my cherrypy to it, so that my wsgi.pyscript define an application object. Also, cherrypy has a websocket tool, as defined by ws4py.

This is a minimal cherrypy application that works under WSGI in OpenShift, and that should use websockets too!

import cherrypy
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
from ws4py.websocket import EchoWebSocket
import atexit
import logging

# see http://tools.cherrypy.org/wiki/ModWSGI
cherrypy.config.update({'environment': 'embedded'}) 
if cherrypy.__version__.startswith('3.0') and cherrypy.engine.state == 0:
    cherrypy.engine.start(blocking=False)
    atexit.register(cherrypy.engine.stop)

class Root(object):
    def index(self): return 'I work!'
    def ws(self): print('THIS IS NEVER PRINTED :(')
    index.exposed=True
    ws.exposed=True

# registering the websocket
conf={'/ws':{'tools.websocket.on': True,'tools.websocket.handler_cls': EchoWebSocket}}
WebSocketPlugin(cherrypy.engine).subscribe()
cherrypy.tools.websocket = WebSocketTool()  

#show stacktraces in console (for some reason this is not default in cherrypy+WSGI)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
stream = logging.StreamHandler()
stream.setLevel(logging.INFO)
logger.addHandler(stream)

application = cherrypy.Application(Root(), script_name='', config=conf)

Everything work wonderfully, except when I create a websocket ( connecting to ws://myserver:8000/ws ), this is the stacktrace I get:

 cherrypy/_cplogging.py, 214, HTTP Traceback (most recent call last):
   File "cherrypy/_cprequest.py", line 661, in respond
     self.hooks.run('before_request_body')
   File "cherrypy/_cprequest.py", line 114, in run
     raise exc
   File "cherrypy/_cprequest.py", line 104, in run
     hook()
   File "cherrypy/_cprequest.py", line 63, in __call__
     return self.callback(**self.kwargs)
   File "ws4py/server/cherrypyserver.py", line 200, in upgrade
     ws_conn = get_connection(request.rfile.rfile)
AttributeError: 'mod_wsgi.Input' object has no attribute 'rfile'

(I manually deleted the absolute path from the filenames) PS: I use python3.3, cherrypy==3.5.0, ws4py==0.3.4.

It is not clear to me:

  • if this is a lack of compatibility between cherrypy and ws4py when in an WSGI environment.
  • if it is a problem of ws4py when in a WSGI environment
  • if it is because Openshift websockets have a different port than the http one

PPS: this is a complete OpenShift project, that you can run and try this yourself: https://github.com/spocchio/wsgi-cherrypy-ws4py

1 Answer 1

1

I don't think it is possible at all. WSGI is a synchronous protocol (1, 2), WebSocket protocol is asynchronous. Wiki states that for a Python application interface OpenShift uses WSGI (3). Alas.

However I've recently played with ws4py in pub/sub scenario and it works really well on top of CherryPy standard HTTP-server deployment. So it shouldn't be a problem on a generic virtual server with no application interface constraints.

4
  • I know, on Heroku it works perfectly. Unlucky they have a 55 seconds timeout on websockets, so the client have to keep the connection alive at least once a minute (not the best option for a mobile app that runs in background). Sep 15, 2014 at 18:31
  • Although the WSGI specification technically disallows it, the issue is more to do with the WSGI server being used. The WebSocket code is relying on being used on a WSGI server implemented as a pure Python WSGI server which deals directly with the client socket itself. It then sides steps the WSGI specification and WSGI server and reaches down to grab the actual socket connection for the client and communicates over it directly. In other words, it is using an evil hack. This hack will not work on mod_wsgi as it doesn't expose the original socket connection. Sep 16, 2014 at 6:25
  • To make it clearer I would add that in case of Python Openshift uses Apache webserver thus mod_wsgi for WSGI. What Graham calls "an evil hack" is documented in ws4py CherryPy module.
    – saaj
    Sep 16, 2014 at 8:55
  • I had indeed to perform such a hack, which i wasn't that proud about. Unfortunately, the WSGI spec was written long before WebSocket and didn't consider that use-case. Hopefully, someday it'll be amended to address it. Jan 28, 2015 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.