[Python] flask socketio CORS

Discussão em 'Python' iniciado por Stack, Junho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I've been trying to send data from flask over socket io. I need to access this data from a different origin, but it is giving a CORS error. I have tried using all kinds of cross origin stuff and none of it has worked. Can somebody help with this.

    The view that should be called thought socket io:

    from flask.ext.cors import cross_origin
    @socketio.on('increment',namespace="/api")
    @cross_origin()
    def increment(message):
    number += 1;
    emit('number',{'data':number},broadcast=True)


    Running the server:

    app = Flask(__name__)
    cors = CORS(app,resources={r"/api/*":{"origins":"*"}})
    socketio = SocketIO(app)
    app.debug = True
    app.host = '0.0.0.0'

    socketio.run(app)

    Continue reading...

Compartilhe esta Página