guv
guv copied to clipboard
WSGI websocket: issues getting sock
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 propertysock
-
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.
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.
Thanks.
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 :)
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.