python-socketio
enforces a same origin policy by default. This means that if your socketio server is running on http://myip:5000
, then it will only accept requests from that origin. So in your case, it's blocking requests from your angular frontend because the request is coming from http://myip:3000
Per the python-socketio documentation:
If necessary, the cors_allowed_origins option can be used to allow other origins. This argument can be set to a string to set a single allowed origin, or to a list to allow multiple origins. A special value of '*' can be used to instruct the server to allow all origins, but this should be done with care, as this could make the server vulnerable to Cross-Site Request Forgery (CSRF) attacks.
How to fix:
Initialize your python-socketio
server with a list of origins you want to allow requests from. In this case, that's your angular application and your python application. Use something like the following (assuming your python server is running locally on port 5000 and angular on 3000):
sio = socketio.Server(cors_allowed_origins=['http://myip:5000', 'http://myip:3000'])
And it should now work.
Note: you can also do the following, but this will accept requests from ALL origins, so be careful as it can open you up to CSRF attacks:
sio = socketio.Server(cors_allowed_origins='*')
Additional note: if you're using flask-socketio
(the Flask extension wrapper of python-socketio
), just pass the same cors_allowed_origins
argument into the SocketIO
object when you initialize (or the init_app
method). You'll also need to enable CORS on your Flask application - I'd recommend using and reading the documentation on Flask-CORS extension - it makes it very simple.