miguelgrinberg / Flask-SocketIO

Socket.IO integration for Flask applications.
MIT License
5.37k stars 892 forks source link

Runtime Error #33

Closed Govinda-Fichtner closed 10 years ago

Govinda-Fichtner commented 10 years ago

I just tried to get started with Flask-SocketIO. I cloned the repository and created a virtualenv and installed the packages from requirements.txt.

All went fine and I could start the application. It looks as if the application works from the browser, but I get the following error in the logs. How can I make this work?

127.0.0.1 - - [2014-05-29 15:08:33] "GET /socket.io/?EIO=2&transport=polling&t=1401368913567-200 HTTP/1.1" 500 161 0.001329
Traceback (most recent call last):
  File "/Users/govindaf/workspace/Flask-SocketIO/example/venv/lib/python2.7/site-packages/gevent/pywsgi.py", line 508, in handle_one_response
    self.run_application()
  File "/Users/govindaf/workspace/Flask-SocketIO/example/venv/lib/python2.7/site-packages/gevent/pywsgi.py", line 494, in run_application
    self.result = self.application(self.environ, self.start_response)
  File "/Users/govindaf/workspace/Flask-SocketIO/example/venv/lib/python2.7/site-packages/flask_socketio/__init__.py", line 27, in __call__
    raise RuntimeError('You need to use a gevent-socketio server.')
RuntimeError: You need to use a gevent-socketio server.
{'GATEWAY_INTERFACE': 'CGI/1.1',
 'HTTP_ACCEPT': '*/*',
 'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch',
 'HTTP_ACCEPT_LANGUAGE': 'de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4',
 'HTTP_CONNECTION': 'keep-alive',
 'HTTP_DNT': '1',
 'HTTP_HOST': '127.0.0.1:5000',
 'HTTP_REFERER': 'http://127.0.0.1:5000/',
 'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36',
 'PATH_INFO': '/socket.io/',
 'QUERY_STRING': 'EIO=2&transport=polling&t=1401368919567-201',
 'REMOTE_ADDR': '127.0.0.1',
 'REMOTE_PORT': '57512',
 'REQUEST_METHOD': 'GET',
 'SCRIPT_NAME': '',
 'SERVER_NAME': '1.0.0.127.in-addr.arpa',
 'SERVER_PORT': '5000',
 'SERVER_PROTOCOL': 'HTTP/1.1',
 'SERVER_SOFTWARE': 'gevent/1.0 Python/2.7',
 'wsgi.errors': <open file '<stderr>', mode 'w' at 0x1054c71e0>,
 'wsgi.input': <gevent.pywsgi.Input object at 0x1061bc610>,
 'wsgi.multiprocess': False,
 'wsgi.multithread': False,
 'wsgi.run_once': False,
 'wsgi.url_scheme': 'http',
 'wsgi.version': (1, 0)} failed with RuntimeError

I am using Python 2.7.5 and this is the output of "pip freeze":

Flask==0.10.1
Flask-SocketIO==0.3.7
Jinja2==2.7.2
MarkupSafe==0.18
Werkzeug==0.9.4
gevent==1.0
gevent-socketio==0.3.6
gevent-websocket==0.9.2
greenlet==0.4.2
itsdangerous==0.23
ujson==1.33
wsgiref==0.1.2
miguelgrinberg commented 10 years ago

How did you start the app? The error seems to indicate you are not running with the gevent-socketio server,

Govinda-Fichtner commented 10 years ago

I did start it with

(venv)govindaf@Govindas-MacBook-Pro ~/workspace/Flask-SocketIO/example (master●)$ python app.py
 * Running on http://127.0.0
Govinda-Fichtner commented 10 years ago

What really confuses me is also that in the environment there is this:

'SERVER_SOFTWARE': 'gevent/1.0 Python/2.7'

So it actually seems to use the gevent server. And also I did not change anything from the checkout repository. So I was assuming that just starting app.py should be sufficient. Do I need to do anything else?

miguelgrinberg commented 10 years ago

Hmm. I cannot reproduce this. I just did an install from scratch on my Mac and the example runs perfectly fine. I'm using the MacPorts Python 2.7 for this (I could not install greenlet on the stock Python from OS X, I get compiler errors).

Govinda-Fichtner commented 10 years ago

I just looked through the things I had installed with brew. I found libevent and libev. I removed these and also removed the virtualenv that I created for the sample app. Then I reinstall the requirements.txt and now it seems to work. I am not sure how this can be related. Maybe when some gevent related stuff is compiled it got the wrong libs...

When I want to run the app with gunicorn - how would I configure and start it?

Govinda-Fichtner commented 10 years ago

Miguel, sorry that I took so much of your time when it seems it was the fault of the libs I had installed on my computer not Flask-SocketIO. I really appreciate the time and effort you put into something like Flask-SocketIO!

miguelgrinberg commented 10 years ago

When I want to run the app with gunicorn - how would I configure and start it?

This is in the documentation. Here is an example cmd line:

gunicorn --worker-class socketio.sgunicorn.GeventSocketIOWorker module:app
guilhermebr commented 10 years ago

It's happening the same with me:

(monitor)root@vagrant-debian-wheezy:/var/sites/ipsafe/monitor/src# python server.py Traceback (most recent call last): File "/var/sites/ipsafe/monitor/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 508, in handle_one_response self.run_application() File "/var/sites/ipsafe/monitor/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 494, in run_application self.result = self.application(self.environ, self.start_response) File "/var/sites/ipsafe/monitor/local/lib/python2.7/site-packages/flask_socketio/init.py", line 27, in call raise RuntimeError('You need to use a gevent-socketio server.') RuntimeError: You need to use a gevent-socketio server. {'GATEWAY_INTERFACE': 'CGI/1.1', 'HTTPACCEPT': '/_', 'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8,es;q=0.6,pt;q=0.4', 'HTTP_CONNECTION': 'keep-alive', 'HTTP_COOKIE': 'InfoViewCE_SAPCnt=null; InfoViewCE_SAPSys=null; InfoViewPLATFORMSVC_COOKIE_AUTH=secEnterprise; InfoViewPLATFORMSVC_COOKIE_CMS=BO2008%3A6400; InfoViewPLATFORMSVC_COOKIE_USR=guilherme.bessa%40ipsafe.com.br; umitsessid=10eee8a64ba0d95a013d6c7f546b0f0b; __atuvc=1%7C23; session=.eJw9jr0KgzAYAF-lfLNDNaaK4GaRFhIRQiVZRD9_qqEtRKWi-O5tHTrceMetkDemHu4QjGaqLci7CoIVDiUEoOIzlQ-ulUDCl9SWPbosTuckkzPL0jcTvFOiPbIIQ9gswME0-fjS9fOfSCJNk-yntgtzeK-iC2GZdJlAqmLpKHHVXFSa97cuEe38xeFpuOemoTb7DlDiY0M9D8vSL23qFKcGsSAEtg_epTw0.Bm6JEA.o--S5Gfk9jalWlF_dhPJf8XIrQc; sessionid=jehby3nta6kni5iw65abjjawayfdpk7j; csrftoken=R9tkTmMXIiGK2CbYJPbqz73UFQEvh1BE', 'HTTP_HOST': '127.0.0.1:8080', 'HTTP_REFERER': 'http://127.0.0.1:8080/', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36', 'PATH_INFO': '/socket.io/', 'QUERY_STRING': 'EIO=2&transport=polling&t=1402006999937-32', 'REMOTE_ADDR': '10.0.2.2', 'REMOTE_PORT': '60661', 'REQUEST_METHOD': 'GET', 'SCRIPT_NAME': '', 'SERVER_NAME': 'vagrant-debian-wheezy.vagrantup.com', 'SERVER_PORT': '8080', 'SERVER_PROTOCOL': 'HTTP/1.1', 'SERVER_SOFTWARE': 'gevent/1.0 Python/2.7', 'wsgi.errors': <open file '', mode 'w' at 0x7f352b46f270>, 'wsgi.input': <gevent.pywsgi.Input object at 0x1791fd0>, 'wsgi.multiprocess': False, 'wsgi.multithread': False, 'wsgi.run_once': False, 'wsgi.url_scheme': 'http', 'wsgi.version': (1, 0)} failed with RuntimeError

10.0.2.2 - - [2014-06-05 22:23:20] "GET /socket.io/?EIO=2&transport=polling&t=1402006999937-32 HTTP/1.1" 500 161 0.017343

tito commented 10 years ago

I got the same too. pip freeze (stripped):

gevent==1.0.1
gevent-socketio==0.3.6
gevent-websocket==0.9.3
greenlet==0.4.2
Flask==0.10.1
Flask-SocketIO==0.3.7

The example i'm testing:

from flask import Flask, render_template
from flask.ext.socketio import SocketIO, emit

HTML = '''
<html>
<head>
    <script src="https://cdn.socket.io/socket.io-1.0.4.js"></script>
    <script>
    var socket = io();
    </script>
</head>
<body>
Hello
</body>
</html>
'''

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

@app.route('/')
def index():
    return HTML

@socketio.on('my event', namespace='/test')
def test_message(message):
    emit('my response', {'data': message['data']})

@socketio.on('my broadcast event', namespace='/test')
def test_message(message):
    emit('my response', {'data': message['data']}, broadcast=True)

@socketio.on('connect', namespace='/test')
def test_connect():
    emit('my response', {'data': 'Connected'})

@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected')

if __name__ == '__main__':
    socketio.run(app)

Run with python test.py:

$ python test.py
127.0.0.1 - - [2014-06-08 03:30:17] "GET / HTTP/1.1" 200 288 0.001365
Traceback (most recent call last):
  File "/home/tito/.envs/testsocketio/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 508, in handle_one_response
    self.run_application()
  File "/home/tito/.envs/testsocketio/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 494, in run_application
    self.result = self.application(self.environ, self.start_response)
  File "/home/tito/.envs/testsocketio/local/lib/python2.7/site-packages/flask_socketio/__init__.py", line 27, in __call__
    raise RuntimeError('You need to use a gevent-socketio server.')
RuntimeError: You need to use a gevent-socketio server.
{'GATEWAY_INTERFACE': 'CGI/1.1',
 'HTTP_ACCEPT': '*/*',
 'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch',
 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8,fr;q=0.6',
 'HTTP_CONNECTION': 'keep-alive',
 'HTTP_COOKIE': '_pk_id.7.1fff=6d547a8ce1cb8a2c.1395234273.3.1395962904.1395304768.; csrftoken=JbCP7osa6fmUuzOXYAUE0YjVr6Wll74O; session=eyJjc3JmIjp7IiBiIjoiTWpVNFkyUm1PVFl0WkdFd05DMHhNV1V6TFdFMlpUQXRaalEyWkRBMFpEWTNaRFl6In0sInVzZXJfaWQiOnsiIGIiOiJOVE0zTVRCaVpqYzBNVEF5TmpRellqUm1ZakkxWkRrMyJ9fQ.BlKo6A.IBhrCUBdUQYhRGSg2GDTGecsarI; testsocketio_id=cf856894-4a60-986b-2462-73ad08e9fa82',
 'HTTP_HOST': 'localhost:5000',
 'HTTP_REFERER': 'http://localhost:5000/',
 'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36',
 'PATH_INFO': '/socket.io/',
 'QUERY_STRING': 'EIO=2&transport=polling&t=1402191047419-8',
 'REMOTE_ADDR': '127.0.0.1',
 'REMOTE_PORT': '36811',
 'REQUEST_METHOD': 'GET',
 'SCRIPT_NAME': '',
 'SERVER_NAME': 'localhost',
 'SERVER_PORT': '5000',
 'SERVER_PROTOCOL': 'HTTP/1.1',
 'SERVER_SOFTWARE': 'gevent/1.0 Python/2.7',
 'wsgi.errors': <open file '<stderr>', mode 'w' at 0x7f8dcc7701e0>,
 'wsgi.input': <gevent.pywsgi.Input object at 0x2c9f150>,
 'wsgi.multiprocess': False,
 'wsgi.multithread': False,
 'wsgi.run_once': False,
 'wsgi.url_scheme': 'http',
 'wsgi.version': (1, 0)} failed with RuntimeError

And tried with gunicorn:

$ gunicorn --worker-class socketio.sgunicorn.GeventSocketIOWorker test:app
2014-06-08 03:31:11 [11457] [INFO] Starting gunicorn 18.0
2014-06-08 03:31:11 [11457] [INFO] Listening at: http://127.0.0.1:8000 (11457)
2014-06-08 03:31:11 [11457] [INFO] Using worker: socketio.sgunicorn.GeventSocketIOWorker
2014-06-08 03:31:11 [11462] [INFO] Booting worker with pid: 11462
Traceback (most recent call last):
  File "/home/tito/.envs/testsocketio/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 508, in handle_one_response
    self.run_application()
  File "/home/tito/.envs/testsocketio/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 494, in run_application
    self.result = self.application(self.environ, self.start_response)
  File "/home/tito/.envs/testsocketio/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/tito/.envs/testsocketio/local/lib/python2.7/site-packages/flask_socketio/__init__.py", line 27, in __call__
    raise RuntimeError('You need to use a gevent-socketio server.')
RuntimeError: You need to use a gevent-socketio server.
{'GATEWAY_INTERFACE': 'CGI/1.1',
 'HTTP_ACCEPT': '*/*',
 'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch',
 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8,fr;q=0.6',
 'HTTP_CONNECTION': 'keep-alive',
 'HTTP_COOKIE': '_pk_id.7.1fff=6d547a8ce1cb8a2c.1395234273.3.1395962904.1395304768.; csrftoken=JbCP7osa6fmUuzOXYAUE0YjVr6Wll74O; session=eyJjc3JmIjp7IiBiIjoiTWpVNFkyUm1PVFl0WkdFd05DMHhNV1V6TFdFMlpUQXRaalEyWkRBMFpEWTNaRFl6In0sInVzZXJfaWQiOnsiIGIiOiJOVE0zTVRCaVpqYzBNVEF5TmpRellqUm1ZakkxWkRrMyJ9fQ.BlKo6A.IBhrCUBdUQYhRGSg2GDTGecsarI; testsocketio_id=cf856894-4a60-986b-2462-73ad08e9fa82',
 'HTTP_HOST': 'localhost:8000',
 'HTTP_REFERER': 'http://localhost:8000/',
 'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36',
 'PATH_INFO': '/socket.io/',
 'QUERY_STRING': 'EIO=2&transport=polling&t=1402191082812-0',
 'RAW_URI': '/socket.io/?EIO=2&transport=polling&t=1402191082812-0',
 'REMOTE_ADDR': '127.0.0.1',
 'REMOTE_PORT': '35369',
 'REQUEST_METHOD': 'GET',
 'SCRIPT_NAME': '',
 'SERVER_NAME': 'localhost',
 'SERVER_PORT': '8000',
 'SERVER_PROTOCOL': 'HTTP/1.1',
 'SERVER_SOFTWARE': 'gevent/1.0 Python/2.7',
 'gunicorn.sock': <socket at 0x1607490 fileno=12 sock=127.0.0.1:8000 peer=127.0.0.1:35369>,
 'wsgi.errors': <open file '<stderr>', mode 'w' at 0x7f0aa077d1e0>,
 'wsgi.input': <gevent.pywsgi.Input object at 0x16246d0>,
 'wsgi.multiprocess': False,
 'wsgi.multithread': False,
 'wsgi.run_once': False,
 'wsgi.url_scheme': 'http',
 'wsgi.version': (1, 0)} failed with RuntimeError
miguelgrinberg commented 10 years ago

Please do not use the new socket.io 1.0 client libraries, that is a very recent release that is likely not supported by project gevent-socketio. Please use 0.9.16 for now.

tito commented 10 years ago

That was the issue. Using 0.9.16 client library works perfectly. Thanks!

gu-fan commented 10 years ago

Met the same issue. Addressed in this thread , Thank you

JanBenes commented 10 years ago

Had the same issue with socket.io.1.1, 0.9.16 works for me

djagdish commented 9 years ago

After several hours of trying to debug this by wrapping the regular Flask app in different gevent supported wsgi servers, simply changing the client library to an older version solved the issue.

0.9.16 is available here: http://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js

dancompton commented 9 years ago

0.9.17 also appears to cause failure