librelist archives

« back to archive

Flask and websockets

Flask and websockets

From:
bruce bushby
Date:
2010-09-01 @ 09:35
Hi

Has Flask or Werkzeug implemented websockets? Any examples about?

I'm using the latest version of Gunicorn, Flask and Werkzeug.


Thanks
Bruce

Re: [flask] Flask and websockets

From:
Daniel Neuhäuser
Date:
2010-09-01 @ 14:22
Neither Werkzeug nor Flask has support for Websockets. The problem here
is that the underlying interface to the webserver which is commonly used
by Python web applications, WSGI, doesn't support websockets. Due to the
nature of WSGI which is intended to run on as much servers as possible I
doubt that WSGI will support websockets at any point in the future.

Re: [flask] Flask and websockets

From:
bruce bushby
Date:
2010-09-01 @ 14:52
Hi Daniel

Thanks for the response!!! I've been following this link:

http://pypi.python.org/pypi/gevent-websocket/0.2.1


[root@ahlx ahldev]# cat

/home/bbushby/Downloads/gevent/gevent-websocket-0.2.1/example/gunicorn_websocket.py

# -*- coding: utf-8 -
#
# gunicorn -k "geventwebsocket.gunicorn.workers.GeventWebSocketWorker"
gunicorn_websocket:app

import gevent

# demo app
import os
import random
def app(environ, start_response):
    if environ['PATH_INFO'] == '/test':
        start_response("200 OK", [('Content-Type', 'text/plain')])
        return ["blaat"]
    elif environ['PATH_INFO'] == "/data":
        ws = environ['wsgi.websocket']
        for i in xrange(10000):
            ws.send("0 %s %s\n" % (i, random.random()))
            gevent.sleep(1)
    else:
        start_response("404 Not Found", [])
        return []

[root@ahlx ahldev]#



but I can't seem to get anything to work :)

How hard would it be for me to "hack" werkzeug or flask to implement the
above?  I use Gunicorn etc.....just would love to play with
websockets....even if it's just "cow boy patch"





2010/9/1 Daniel Neuhäuser <dasdasich@googlemail.com>

> Neither Werkzeug nor Flask has support for Websockets. The problem here
> is that the underlying interface to the webserver which is commonly used
> by Python web applications, WSGI, doesn't support websockets. Due to the
> nature of WSGI which is intended to run on as much servers as possible I
> doubt that WSGI will support websockets at any point in the future.
>
>

Re: [flask] Flask and websockets

From:
Daniel Neuhäuser
Date:
2010-09-01 @ 16:14

> How hard would it be for me to "hack" werkzeug or flask to implement
> the above?  I use Gunicorn etc.....just would love to play with
> websockets....even if it's just "cow boy patch"
You can simply access request.environ['wsgi.websocket']

Re: [flask] Flask and websockets

From:
bruce bushby
Date:
2010-09-01 @ 16:34
I switched over to focus on the examples which appears to show a bug
in gevent-websocket-0.2.1.
After installing
the latest everything (libevent, greenlet, Gunicorn and
gevent-websocket-0.2.1)
I started Gunicorn using the suggested command line.....however it fails
when http://127.0.0.1:8000/data

The error indicates the environ dict entry "wsgi.websocket" is never created
causing Gunicorn to complain about the missing dict key (KeyError:
'wsgi.websocket') ..... at least that my limited understanding of what is
going on.

I'll see if I can pester Jeff for some help and update the list if it works.


*Session extract:*

[root@ahlx tmp]#
[root@ahlx tmp]# pwd
/tmp
[root@ahlx tmp]#
[root@ahlx tmp]# ls -l gunicorn_websocket.py
-rw-r--r-- 1 root root 605 Sep  1 16:00 gunicorn_websocket.py
[root@ahlx tmp]#
[root@ahlx tmp]#
[root@ahlx tmp]# cat gunicorn_websocket.py
# -*- coding: utf-8 -
#
# gunicorn -k "geventwebsocket.gunicorn.workers.GeventWebSocketWorker"
gunicorn_websocket:app

import gevent

# demo app
import os
import random
def app(environ, start_response):
    if environ['PATH_INFO'] == '/test':
        start_response("200 OK", [('Content-Type', 'text/plain')])
        return ["blaat"]
    elif environ['PATH_INFO'] == "/data":
        ws = environ['wsgi.websocket']
        for i in xrange(10000):
            ws.send("0 %s %s\n" % (i, random.random()))
            gevent.sleep(1)
    else:
        start_response("404 Not Found", [])
        return []

[root@ahlx tmp]#
[root@ahlx tmp]#
[root@ahlx tmp]#
[root@ahlx tmp]# gunicorn -k
"geventwebsocket.gunicorn.workers.GeventWebSocketWorker"
gunicorn_websocket:app
2010-09-01 17:23:27 [7881] [INFO] Starting gunicorn 0.11.0
2010-09-01 17:23:27 [7881] [INFO] Listening at: http://127.0.0.1:8000
2010-09-01 17:23:27 [7883] [INFO] Booting worker with pid: 7883
127.0.0.1 - - [2010-09-01 17:23:36] "GET /test HTTP/1.1" 200 106 0.000200
Traceback (most recent call last):
  File "/usr/lib64/python2.6/site-packages/gevent/pywsgi.py", line 386, in
handle_one_response
    self.result = self.application(self.environ, self.start_response)
  File "/tmp/gunicorn_websocket.py", line 15, in app
    ws = environ['wsgi.websocket']
KeyError: 'wsgi.websocket'
<PyWSGIServer fileno=9 address=127.0.0.1:8000>: Failed to handle request:
  request = GET /data HTTP/1.1 from ('127.0.0.1', 34313)
  application = <function app at 0x225c758>

127.0.0.1 - - [2010-09-01 17:23:40] "GET /data HTTP/1.1" 500 161 0.000731
^C2010-09-01 17:23:43 [7883] [INFO] Worker exiting (pid: 7883)
[root@ahlx tmp]#





Cheers for  the help!!!



2010/9/1 Daniel Neuhäuser <dasdasich@googlemail.com>

>
>
> > How hard would it be for me to "hack" werkzeug or flask to implement
> > the above?  I use Gunicorn etc.....just would love to play with
> > websockets....even if it's just "cow boy patch"
> You can simply access request.environ['wsgi.websocket']
>
>
>

Re: [flask] Flask and websockets

From:
bruce bushby
Date:
2010-09-01 @ 21:20
Hi

Jeff Gelens (http://pypi.python.org/pypi/gevent-websocket/0.2.1) replied and
explained what I was doing wrong, the setup is very simple:

1. Install  
gevent-websocket-0.2.1<http://pypi.python.org/pypi/gevent-websocket/0.2.1>
2. cd to 
gevent-websocket-0.2.1<http://pypi.python.org/pypi/gevent-websocket/0.2.1>
/examples
3. run gunicorn -k "geventwebsocket.gunicorn.workers.GeventWebSocketWorker"
gunicorn_websocket:app
4. point google-chrome browser to
gevent-websocket-0.2.1<http://pypi.python.org/pypi/gevent-websocket/0.2.1>
/examples/websocket.html

and thats it, a nice little "plot" graph auto-updates using websockets :)

I guess the above example has nothing to do with Flask but it's nice to know
it's all there.

Bruce



On Wed, Sep 1, 2010 at 5:34 PM, bruce bushby <bruce.bushby@googlemail.com>wrote:

>
>
> I switched over to focus on the examples which appears to show a bug in 
gevent-websocket-0.2.1.
> After installing
> the latest everything (libevent, greenlet, Gunicorn and gevent-websocket-0.2.1)
> I started Gunicorn using the suggested command line.....however it fails
> when http://127.0.0.1:8000/data
>
> The error indicates the environ dict entry "wsgi.websocket" is never
> created causing Gunicorn to complain about the missing dict key (KeyError:
> 'wsgi.websocket') ..... at least that my limited understanding of what is
> going on.
>
> I'll see if I can pester Jeff for some help and update the list if it
> works.
>
>
> *Session extract:*
>
> [root@ahlx tmp]#
> [root@ahlx tmp]# pwd
> /tmp
> [root@ahlx tmp]#
> [root@ahlx tmp]# ls -l gunicorn_websocket.py
> -rw-r--r-- 1 root root 605 Sep  1 16:00 gunicorn_websocket.py
> [root@ahlx tmp]#
> [root@ahlx tmp]#
> [root@ahlx tmp]# cat gunicorn_websocket.py
>
> # -*- coding: utf-8 -
> #
> # gunicorn -k "geventwebsocket.gunicorn.workers.GeventWebSocketWorker"
> gunicorn_websocket:app
>
> import gevent
>
> # demo app
> import os
> import random
> def app(environ, start_response):
>     if environ['PATH_INFO'] == '/test':
>         start_response("200 OK", [('Content-Type', 'text/plain')])
>         return ["blaat"]
>     elif environ['PATH_INFO'] == "/data":
>         ws = environ['wsgi.websocket']
>         for i in xrange(10000):
>             ws.send("0 %s %s\n" % (i, random.random()))
>             gevent.sleep(1)
>     else:
>         start_response("404 Not Found", [])
>         return []
>
> [root@ahlx tmp]#
> [root@ahlx tmp]#
> [root@ahlx tmp]#
> [root@ahlx tmp]# gunicorn -k
> "geventwebsocket.gunicorn.workers.GeventWebSocketWorker"
> gunicorn_websocket:app
> 2010-09-01 17:23:27 [7881] [INFO] Starting gunicorn 0.11.0
> 2010-09-01 17:23:27 [7881] [INFO] Listening at: http://127.0.0.1:8000
> 2010-09-01 17:23:27 [7883] [INFO] Booting worker with pid: 7883
> 127.0.0.1 - - [2010-09-01 17:23:36] "GET /test HTTP/1.1" 200 106 0.000200
> Traceback (most recent call last):
>   File "/usr/lib64/python2.6/site-packages/gevent/pywsgi.py", line 386, in
> handle_one_response
>     self.result = self.application(self.environ, self.start_response)
>   File "/tmp/gunicorn_websocket.py", line 15, in app
>     ws = environ['wsgi.websocket']
> KeyError: 'wsgi.websocket'
> <PyWSGIServer fileno=9 address=127.0.0.1:8000>: Failed to handle request:
>   request = GET /data HTTP/1.1 from ('127.0.0.1', 34313)
>   application = <function app at 0x225c758>
>
> 127.0.0.1 - - [2010-09-01 17:23:40] "GET /data HTTP/1.1" 500 161 0.000731
> ^C2010-09-01 17:23:43 [7883] [INFO] Worker exiting (pid: 7883)
> [root@ahlx tmp]#
>
>
>
>
>
> Cheers for  the help!!!
>
>
>
> 2010/9/1 Daniel Neuhäuser <dasdasich@googlemail.com>
>
>>
>>
>> > How hard would it be for me to "hack" werkzeug or flask to implement
>> > the above?  I use Gunicorn etc.....just would love to play with
>> > websockets....even if it's just "cow boy patch"
>> You can simply access request.environ['wsgi.websocket']
>>
>>
>>
>

Re: [flask] Flask and websockets

From:
Ali Afshar
Date:
2010-09-01 @ 14:27
2010/9/1 Daniel Neuhäuser <dasdasich@googlemail.com>:
> Neither Werkzeug nor Flask has support for Websockets. The problem here
> is that the underlying interface to the webserver which is commonly used
> by Python web applications, WSGI, doesn't support websockets. Due to the
> nature of WSGI which is intended to run on as much servers as possible I
> doubt that WSGI will support websockets at any point in the future.
>

Whilst this is true, it doesn't necessarily follow that Flask should
be a wsgi-only framework.

Re: [flask] Flask and websockets

From:
Dan Jacob
Date:
2010-09-01 @ 15:19
I wonder if it would be possible to integrate Flask with a
non-blocking server such as Tornado ?

On 1 September 2010 15:27, Ali Afshar <aafshar@gmail.com> wrote:
> 2010/9/1 Daniel Neuhäuser <dasdasich@googlemail.com>:
>> Neither Werkzeug nor Flask has support for Websockets. The problem here
>> is that the underlying interface to the webserver which is commonly used
>> by Python web applications, WSGI, doesn't support websockets. Due to the
>> nature of WSGI which is intended to run on as much servers as possible I
>> doubt that WSGI will support websockets at any point in the future.
>>
>
> Whilst this is true, it doesn't necessarily follow that Flask should
> be a wsgi-only framework.
>