guv icon indicating copy to clipboard operation
guv copied to clipboard

WSGI websocket: issues getting sock

Open dmdm opened this issue 9 years ago • 4 comments

I'm have a simple WebsocketWSGI server:


@guv.websocket.WebSocketWSGI
def my_handler(ws):
    print('INSIDE HANDLER')
    while True:
        from_browser = ws.wait()
        print('>', from_browser)
        ws.send("from server:" + from_browser)

# Served by gunicorn with guv.GuvWorker
def main(global_config, **settings):
    lgg = logging.getLogger('root')
    lgg.info('start')
    return my_handler

# Standalone
def serve():
    init_standalone()
    lgg = logging.getLogger('root')
    lgg.info('start')
    host = RC.g('server.host')
    port = int(RC.g('server.port'))
    server_sock = guv.listen((host, port))
    guv.wsgi.serve(server_sock, my_handler)

if __name__ == '__main__':
    serve()

At some point WebSocketWSGI tries to obtain the socket from the WSGI environment. The original code did not work for multiple reasons:

  • Key guv.input does not exist
  • There's key wsgi.input
  • A method get_sock() does not exist at all, but guv's class Input has property sock
  • wsgi.input is set to different objects, depending on whether gunicorn or guv serves.

Please have a look at this diff. Between some debug prints, the two important changes are to force Input to always set the sock (here) and the method how to fetch the socket from environ (here).

I am not sure what the intended behaviour actually should be.

dmdm avatar Jul 10 '15 16:07 dmdm

This module is actually taken from gevent or eventlet (most likely an old eventlet version). I've never used it myself, so I'm not sure what the intended behaviour should be. I'll take a look and see if I can fix it.

veegee avatar Jul 11 '15 02:07 veegee

Thanks.

dmdm avatar Jul 11 '15 09:07 dmdm

Currently working on fixing this. If you could come up with some test cases, aside from the issue itself, or better yet write some py.test code, that would be an enormous help :)

veegee avatar Jul 14 '15 00:07 veegee

Ok, I will see to it, but alas, it will be next weekend at the earliest. Had encountered some other issues in the meantime, will document them then too.

dmdm avatar Jul 14 '15 06:07 dmdm