How Can I Connect A Javascript Client To A Python-SocketIO Server?

- 1 answer

I'm just getting started to Python-SocketIO, i created an example server, it looks like this:

import eventlet
import socketio

sio = socketio.Server()

app = socketio.WSGIApp(sio, static_files={
    '/': {'content_type': 'text/html', 'filename': 'index.html'}

def connect(sid, environ):
    print('connect ', sid)

def message(sid, data):
    print('message ', data)

def disconnect(sid):
    print('disconnect ', sid)

if __name__ == '__main__':
    eventlet.wsgi.server(eventlet.listen(('', 5000)), app)

Then, i have an HTML file which is running on a Django application, i would like to connect that client to my Python-SocketIO server:

<script src=""></script>

socket = io.connect('');

socket.on('connect',function() {
  console.log('Client has connected to the server!');

socket.on('msg',function(data) {
  console.log('Received a message from the server!',data);

socket.on('disconnect',function() {
  console.log('The client has disconnected!');

// Sends a message to the server via sockets
function send(message) {


The problem is that, on my client side, i keep getting the following errors:

Access to XMLHttpRequest at 'http://localhost:5000/' from origin '' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
localhost:5000/ Failed to load resource: net::ERR_FAILED

My Django server is running on Can anyone help me find what i'm doing wrong?



Set the CORS header on your server:

sio = socketio.Server(cors_allowed_origins='*')