How to run flask socket.io on localhost (xampp)

The tutorials I've seen use the following code to start the server:

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

      

My __init__.py

file:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.orm import sessionmaker
from sqlalchemy import *
from flask.ext.socketio import SocketIO, emit                                                                                       


app = Flask(__name__)
socketio = SocketIO(app)
app.debug = True
engine = create_engine('mysql://root:my_pw@localhost/db_name') 
DBSession = sessionmaker(bind=engine)
import couponmonk.views

      

My file views.py

contains all decoders @app.route

and @socketio

.

My question is where should I place the code:

socketio.run(app)

      

When I put it in a file __init__.py_

, I get errors:

File "/opt/lampp/htdocs/flaskapp/flask.wsgi", line 7, in <module>
from couponmonk import app as application
File "/home/giri/Desktop/couponmonk/venv/couponmonk/__init__.py", line 14, in <module>
socketio.run(app)
File "/home/giri/Desktop/couponmonk/venv/lib/python2.7/site-packages/flask_socketio/__init__.py", line 411, in run
run_with_reloader(run_server)
File "/home/giri/Desktop/couponmonk/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 632, in run_with_reloader
return run_with_reloader(*args, **kwargs)
File "/home/giri/Desktop/couponmonk/venv/lib/python2.7/site-packages/werkzeug/_reloader.py", line 231, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
SystemExit: 2

      

+3


source to share


2 answers


The author of Flask-SocketIO is here.

Unfortunately this extension cannot work with a standard webserver, you cannot host an application using it on top of apache / mod_wsgi. You need to use the gevent server, not the generic one, but the one configured for Socket.IO.

This means Apache is out (it doesn't even support WebSocket traffic). Also uWSGI is missing (gevent supported, but not possible to use native gevent server). As a side note, Python 3 is also missing, as gevent only works on Python 2 (although I think there will be good news about it soon, I'm working on some ideas to get the socket running on Python 3 right now).

The choice you have is given in the documentation . Short description:



  • socketio.run(app)

    which runs its own socketio gevent server directly.
  • Gunicorn with custom socket worker (command line shown in docs)

You can put nginx as a reverse proxy in front of your server if you like. The configuration is also shown in the docs.

Good luck!

+1


source


It seems like lime you are trying to use the Miguel Flask-socketIO extension, right? It only supports Guinicorn as a WSGI server and advises using NGINX as a proxy. I don't know anything about xampp, but as far as I read; It is possible to do proxy pass with one of the latest Apache versions. Haven't tried it though.



0


source







All Articles