serve kubernetes exec websocket

81 views
Skip to first unread message

Jorge

unread,
Jun 18, 2019, 12:02:04 PM6/18/19
to gevent: coroutine-based Python network library
Hello,

I would like to serve my kubernetes exec websocket through gevent-websocket, but I'm not sure how I can do it.
In other words, I want to serve my kubernetes.stream.ws_client.WSClient instance, which contains a websocket.WebSocket object, through a flask app by using the request.environment['wsgi.websocket'] object.

Many thanks,

Jorge

Matt Billenstein

unread,
Jun 18, 2019, 8:23:37 PM6/18/19
to gev...@googlegroups.com
Hi, I've used gevent-websocket and gunicorn for this -- so in the gunicorn
config you use a GeventWebSocketWorker:


```
$ grep worker_class webapp/gconfig.py
worker_class = 'geventwebsocket.gunicorn.workers.GeventWebSocketWorker'

$ cat webapp/run.sh
#!/bin/bash

pushd $(dirname $0) > /dev/null
HERE="$(pwd)"
popd > /dev/null

exec gunicorn -c $HERE/gconfig.py $* main:app
```

Then in flask you define a route and a handler:

```
from flask import request, Flask

app = Flask(__name__)

def handler():
ws = request.environ['wsgi.websocket']
while 1:
msg = ws.receive()
ws.send(msg)

app.route('/ws/')(handler)
```

More or less -- code I mocked here is untested...

m
> --
> You received this message because you are subscribed to the Google Groups
> "gevent: coroutine-based Python network library" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to gevent+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/
> gevent/975bcabc-dc59-4b12-95cb-3795ff6e39a0%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


--
Matt Billenstein
ma...@vazor.com
http://www.vazor.com/
Reply all
Reply to author
Forward
0 new messages