1

socketio can't get the data from python to angular front end it shows the error as "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://my ip:3000/socket.io/?EIO=3&transport=polling&t=NQtJeHE. (Reason: CORS request did not succeed)"

while True:
    sio.emit('livedata', {data'})

Didn't get this into angular from python socketio emits shows the above error

Irfan wani
  • 4,084
  • 2
  • 19
  • 34

2 Answers2

1

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.

riptusk331
  • 369
  • 4
  • 9
  • How this emit work to a specific port using socket io server it works with client? – Atul T Varghese Jan 01 '21 at 07:22
  • Sorry, I don't understand what you're asking. SocketIO will emit to the clients that are connected (whatever their port is)...it's up to you to specify who is allowed to connect when you initialize the server...which I described to you above. The reason it's not working now is because the default SocketIO server configuration doesn't allow `http://myip:3000` to connect, as it's outside the origin your Python server is running on (most likely `http://myip:5000`). – riptusk331 Jan 01 '21 at 14:55
  • By the server can't handle more than one connection at a time why? I mention socket.listen(100) but a connection is happening at a time only for one endpoint – Atul T Varghese Jan 02 '21 at 08:48
0

Using async mode threading for multiprocessing and cros for the security can use

cors_allowed_origins=['*'] for all

import threading
socketio = SocketIO(app,cors_allowed_origins=['http://pythonserver-ip:port','http://angular-ip:4200'],async_mode='threading')

for sleep in while loop use this code instead of time.sleep(2)

socketio.sleep(2)