Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: You need to use a gevent-socketio server. #102

Closed
jasdevsidhu opened this issue Mar 20, 2015 · 17 comments
Closed

RuntimeError: You need to use a gevent-socketio server. #102

jasdevsidhu opened this issue Mar 20, 2015 · 17 comments
Labels

Comments

@jasdevsidhu
Copy link

Hi there I followed all the steps to used flask.sockets.io but I am facing the following problem
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/Flask-0.10.1-py2.7.egg/flask/app.py", line 1836, in call
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-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.

Can anyone help me out.

@miguelgrinberg
Copy link
Owner

How are you starting your server? You can't do app.run(), you have to use socketio.run() as shown in the documentation and examples.

@jasdevsidhu
Copy link
Author

Oops so sorry, I guest I didn't read the documentation properly. Now my application works fine with socket.io locally and thank you very much for providing flask-socket.io. It is extremely easy to use and thank you once again

@jasdevsidhu
Copy link
Author

Hi There,
When I run my application in pythonanywhere server, I set the following in my wsgi.py file
main2.py is my main file
#-----------------------------------------------------------------------------------------------------------------------------
import os
import sys
path = '/home/jasdevsidhu14/socket'
if path not in sys.path:
sys.path.append(path)

from main2 import socketio as application

def application(environ, start_response):
if environ.get('PATH_INFO') == '/':
status = '200 OK'
content = HELLO_WORLD
else:
status = '404 NOT FOUND'
content = 'Page not found.'
response_headers = [('Content-Type', 'text/html'), ('Content-Length', str(len(content)))]
start_response(status, response_headers)
yield content.encode('utf8')
#-----------------------------------------------------------------------------------------------------------------------------
and I get the following error

2015-03-22 20:53:15,827 :Traceback (most recent call last):
2015-03-22 20:53:15,827 : File "/bin/user_wsgi_wrapper.py", line 130, in call
2015-03-22 20:53:15,827 : self.error_log_file.logger.exception("Error running WSGI application")
2015-03-22 20:53:15,827 : File "/usr/lib/python2.7/logging/init.py", line 1185, in exception
2015-03-22 20:53:15,827 : self.error(msg, _args, *_kwargs)
2015-03-22 20:53:15,827 : File "/usr/lib/python2.7/logging/init.py", line 1178, in error
2015-03-22 20:53:15,828 : self._log(ERROR, msg, args, **kwargs)
2015-03-22 20:53:15,828 : File "/usr/lib/python2.7/logging/init.py", line 1270, in _log
2015-03-22 20:53:15,828 : record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2015-03-22 20:53:15,828 : File "/usr/lib/python2.7/logging/init.py", line 1244, in makeRecord
2015-03-22 20:53:15,828 : rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2015-03-22 20:53:15,828 : File "/usr/lib/python2.7/logging/init.py", line 284, in init
2015-03-22 20:53:15,829 : self.threadName = threading.current_thread().name
2015-03-22 20:53:15,829 : File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
2015-03-22 20:53:15,829 : return _active[_get_ident()]
2015-03-22 20:53:15,829 : File "/bin/user_wsgi_wrapper.py", line 122, in call
2015-03-22 20:53:15,829 : app_iterator = self.app(environ, start_response)
2015-03-22 20:53:15,829 :TypeError: 'SocketIO' object is not callable

How should I create wsgi.py file to run socketio.
Thank you in advance

@miguelgrinberg
Copy link
Owner

Based on this thread it appears pythonanywhere does not support the WebSocket protocol.

@jasdevsidhu
Copy link
Author

Hi There,
I'm complete beginner in this field, especially deploying my project to the cloud server.
Can I know which server providers will be able to support WebSocket protocol since pythonanywhere don't. Does http://www.ovh.ie/vps/vps-classic.xml support WebSocket protocol so that I can upload my project.
I truly appologise if this question irritates you.
Thank you in advance.

@miguelgrinberg
Copy link
Owner

So basically, any plan that gives you a server, either virtual or physical, is going to work fine because you have full control of your stack, you just get a machine and you can install whatever you want in it.

What you get from pythonanywhere is different. Here you don't get full control of a server, you just install your application on a server that is controlled by them. So here the stack is is chosen by the service provider not by you. So while this has some restrictions, it is much easier to setup an application because you don't have to worry about setting up the server.

If you are looking for a service similar to pythonanywhere that has support for WebSocket, then I recommend you check out Heroku. They have a free tier so you can try before you buy.

@jasdevsidhu
Copy link
Author

Traceback (most recent call last):
[Mon Mar 23 20:42:11.285784 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in
[Mon Mar 23 20:42:11.285916 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] from test import socketio as application
[Mon Mar 23 20:42:11.286012 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] File "/var/www/FlaskApp/test/init.py", line 33, in
[Mon Mar 23 20:42:11.286103 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] socketio.run(app,host='5.196.12.71',port=80)
[Mon Mar 23 20:42:11.286122 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] File "/usr/local/lib/python2.7/dist-packages/flask_socketio/init.py", line 414, in run
[Mon Mar 23 20:42:11.286456 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] self.server.serve_forever()
[Mon Mar 23 20:42:11.286482 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] File "/usr/local/lib/python2.7/dist-packages/gevent/baseserver.py", line 282, in serve_forever
[Mon Mar 23 20:42:11.286669 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] self.start()
[Mon Mar 23 20:42:11.286688 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] File "/usr/local/lib/python2.7/dist-packages/gevent/baseserver.py", line 234, in start
[Mon Mar 23 20:42:11.286718 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] self.init_socket()
[Mon Mar 23 20:42:11.286732 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 639, in init_socket
[Mon Mar 23 20:42:11.287084 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] StreamServer.init_socket(self)
[Mon Mar 23 20:42:11.287111 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] File "/usr/local/lib/python2.7/dist-packages/gevent/server.py", line 78, in init_socket
[Mon Mar 23 20:42:11.287252 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] self.socket = self.get_listener(self.address, self.backlog, self.family)
[Mon Mar 23 20:42:11.287270 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] File "/usr/local/lib/python2.7/dist-packages/gevent/server.py", line 89, in get_listener
[Mon Mar 23 20:42:11.287298 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] return _tcp_listener(address, backlog=backlog, reuse_addr=self.reuse_addr, family=family)
[Mon Mar 23 20:42:11.287312 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] File "/usr/local/lib/python2.7/dist-packages/gevent/server.py", line 153, in _tcp_listener
[Mon Mar 23 20:42:11.287333 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] sock.bind(address)
[Mon Mar 23 20:42:11.287346 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] File "", line 1, in bind
[Mon Mar 23 20:42:11.287385 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] error: [Errno 13] Permission denied: ('5.196.12.71', 80)
Hi there,
I am receiving the above error, can anyone help me out

@miguelgrinberg
Copy link
Owner

Okay, this is all expected.

[Mon Mar 23 20:42:11.287385 2015] [:error] [pid 23001:tid 139974057916160] [client 89.101.241.102:10148] error: [Errno 13] Permission denied: ('5.196.12.71', 80)

The above error occurs because you are trying to open port 80 and you are not the root user. In Unix only the root user can access ports below 1024.

[Mon Mar 23 21:03:15.613154 2015] [:error] [pid 23281:tid 139995971045120] [client 54.166.178.233:53904] error: [Errno 98] Address already in use: ('127.0.0.1', 5000)

This error suggests you have another service that is running on port 5000. Maybe another Flask application?

@jasdevsidhu
Copy link
Author

if I set socketio.run(app) by default, I will get the following error

[Mon Mar 23 22:13:50.897494 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] mod_wsgi (pid=24826): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Mon Mar 23 22:13:50.897531 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] Traceback (most recent call last):
[Mon Mar 23 22:13:50.897556 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in
[Mon Mar 23 22:13:50.897653 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] from test import app as application
[Mon Mar 23 22:13:50.897674 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] File "/var/www/FlaskApp/test/init.py", line 33, in
[Mon Mar 23 22:13:50.897737 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] socketio.run(app)
[Mon Mar 23 22:13:50.897755 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] File "/usr/local/lib/python2.7/dist-packages/flask_socketio/init.py", line 414, in run
[Mon Mar 23 22:13:50.898166 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] self.server.serve_forever()
[Mon Mar 23 22:13:50.898196 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] File "/usr/local/lib/python2.7/dist-packages/gevent/baseserver.py", line 282, in serve_forever
[Mon Mar 23 22:13:50.898373 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] self.start()
[Mon Mar 23 22:13:50.898392 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] File "/usr/local/lib/python2.7/dist-packages/gevent/baseserver.py", line 234, in start
[Mon Mar 23 22:13:50.898421 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] self.init_socket()
[Mon Mar 23 22:13:50.898435 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 640, in init_socket
[Mon Mar 23 22:13:50.898794 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] self.update_environ()
[Mon Mar 23 22:13:50.898818 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 647, in update_environ
[Mon Mar 23 22:13:50.898851 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] name = socket.getfqdn(address[0])
[Mon Mar 23 22:13:50.898875 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] File "/usr/local/lib/python2.7/dist-packages/gevent/socket.py", line 645, in getfqdn
[Mon Mar 23 22:13:50.899311 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] hostname, aliases, ipaddrs = gethostbyaddr(name)
[Mon Mar 23 22:13:50.899343 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] File "/usr/local/lib/python2.7/dist-packages/gevent/socket.py", line 625, in gethostbyaddr
[Mon Mar 23 22:13:50.899376 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] return get_hub().resolver.gethostbyaddr(ip_address)
[Mon Mar 23 22:13:50.899391 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] File "/usr/local/lib/python2.7/dist-packages/gevent/resolver_thread.py", line 37, in gethostbyaddr
[Mon Mar 23 22:13:50.899467 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] return self.pool.apply_e(self.expected_errors, _socket.gethostbyaddr, args, kwargs)
[Mon Mar 23 22:13:50.899484 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] File "/usr/local/lib/python2.7/dist-packages/gevent/threadpool.py", line 222, in apply_e
[Mon Mar 23 22:13:50.899662 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] success, result = self.spawn(wrap_errors, expected_errors, function, args, kwargs).get()
[Mon Mar 23 22:13:50.899693 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] File "/usr/local/lib/python2.7/dist-packages/gevent/event.py", line 233, in get
[Mon Mar 23 22:13:50.899886 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] result = self.hub.switch()
[Mon Mar 23 22:13:50.899904 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 331, in switch
[Mon Mar 23 22:13:50.900323 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] return greenlet.switch(self)
[Mon Mar 23 22:13:50.900369 2015] [:error] [pid 24826:tid 140210539063040] [client 89.101.241.102:2765] LoopExit: This operation would block forever

@miguelgrinberg
Copy link
Owner

Not sure about this one. I recommend that you try to run the example app from this repository and see if that one works for you.

@jasdevsidhu
Copy link
Author

I currently running your application, below is the flaskapp.wsgi file
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from test_soc import app as application
application.secret_key = 'Add your secret key'

If I change the 'app' to socketio I will get the following error
[Mon Mar 23 22:57:46.120895 2015] [:error] [pid 25941:tid 140071461680896] [client 89.101.241.102:57436] TypeError: 'SocketIO' object is not callable

and if I don't change, I will get the following error

Traceback (most recent call last):
[Mon Mar 23 23:00:14.154856 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
[Mon Mar 23 23:00:14.154877 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] response = self.make_response(self.handle_exception(e))
[Mon Mar 23 23:00:14.154884 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
[Mon Mar 23 23:00:14.154962 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] reraise(exc_type, exc_value, tb)
[Mon Mar 23 23:00:14.154969 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
[Mon Mar 23 23:00:14.154975 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] response = self.full_dispatch_request()
[Mon Mar 23 23:00:14.154980 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
[Mon Mar 23 23:00:14.154985 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] rv = self.handle_user_exception(e)
[Mon Mar 23 23:00:14.154990 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
[Mon Mar 23 23:00:14.154995 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] reraise(exc_type, exc_value, tb)
[Mon Mar 23 23:00:14.155000 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
[Mon Mar 23 23:00:14.155005 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] rv = self.dispatch_request()
[Mon Mar 23 23:00:14.155010 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
[Mon Mar 23 23:00:14.155015 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] return self.view_functionsrule.endpoint
[Mon Mar 23 23:00:14.155020 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] File "/var/www/FlaskApp/test_soc/init.py", line 33, in index
[Mon Mar 23 23:00:14.155025 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] thread.start()
[Mon Mar 23 23:00:14.155030 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] File "/usr/lib/python2.7/threading.py", line 748, in start
[Mon Mar 23 23:00:14.155035 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] self.__started.wait()
[Mon Mar 23 23:00:14.155039 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] File "/usr/lib/python2.7/threading.py", line 618, in wait
[Mon Mar 23 23:00:14.155044 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] self.__cond.wait(timeout)
[Mon Mar 23 23:00:14.155049 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] File "/usr/lib/python2.7/threading.py", line 339, in wait
[Mon Mar 23 23:00:14.155054 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] waiter.acquire()
[Mon Mar 23 23:00:14.155081 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] File "_semaphore.pyx", line 112, in gevent._semaphore.Semaphore.acquire (gevent/gevent._semaphore.c:3004)
[Mon Mar 23 23:00:14.155099 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942]
[Mon Mar 23 23:00:14.155104 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] File "/usr/local/lib/python2.7/dist-packages/gevent/hub.py", line 331, in switch
[Mon Mar 23 23:00:14.155109 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] return greenlet.switch(self)
[Mon Mar 23 23:00:14.155113 2015] [:error] [pid 26083:tid 139984728225536] [client 89.101.241.102:8942] LoopExit: This operation would block forever

@miguelgrinberg
Copy link
Owner

Why are you creating a wsgi file? Sockets aren't part of WSGI, so you can't work with this extension when only WSGI is supported.

Try running the application by executing app.py. That has the code to correctly setup the gevent server to handle both WSGI and socket traffic.

@jasdevsidhu
Copy link
Author

I'm so sorry if my knowledge on server setup is poor, as I'm running my Flask app with apache2 server with aide of this link:https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps.
How should I setup the server inorder to run pure Flask app

@miguelgrinberg
Copy link
Owner

Sorry this is so complicated. WebSocket is kind of a black sheep, apache2 also does not support this protocol. I recommend that you read the documentation of this extension for the recommended deployment stack. Basically nginx+gunicorn is what I recommend, but if you want to start off without things being so complicated then gunicorn alone is a good choice.

@jasdevsidhu
Copy link
Author

Hi there,
my flask-socketio is perfectly working but it is not using websocket, instead it is using xhr-polling

I receive an error when it uses websocket

File "/usr/local/lib/python2.7/dist-packages/gevent/greenlet.py", line 327, in run
result = self._run(_self.args, *_self.kwargs)
File "/usr/local/lib/python2.7/dist-packages/socketio/server.py", line 124, in handle
handler.handle()
File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 184, in handle
result = self.handle_one_request()
File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 321, in handle_one_request
self.handle_one_response()
File "/usr/local/lib/python2.7/dist-packages/socketio/handler.py", line 170, in handle_one_response
self.transport.do_exchange(socket, request_method)
File "/usr/local/lib/python2.7/dist-packages/socketio/transports.py", line 241, in do_exchange
websocket = self.handler.environ['wsgi.websocket']
KeyError: 'wsgi.websocket'
<Greenlet at 0x21144b0: <bound method SocketIOServer.handle of <SocketIOServer at 0x2149390 fileno=5 address=127.0.0.1:8000>>(<socket at 0x2149290 fileno=[Errno 9] Bad file des, ('127.0.0.1', 58077))> failed with KeyError

@miguelgrinberg
Copy link
Owner

@jasdevsidhu what server and browser are you using for this last test?

@miguelgrinberg
Copy link
Owner

Given than the deployment options are different in release 1.0, I'm going to close this. Please reopen if you have problems with 1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants