Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

524
Vistas
Python Socketio not connecting with JavaScript socket.io-client

I want to simply connect socket.io-client to python server, but for some reason it's keep failing. Initially I start my python server and then try to connect JavaScript as my client server after following that process what I see is JavaScript client server is keep trying to connect and failing with following error:

websocket.js:88 WebSocket connection to 'ws://localhost:5100/socket.io/?EIO=4&transport=websocket' failed:

and python server also repeating following error:

(10448) accepted ('127.0.0.1', 61471) 127.0.0.1 - - [01/Feb/2022 15:14:01] "GET /socket.io/?EIO=4&transport=websocket HTTP/1.1" 400 136 0.000000

I am using python3.10 with socketio following Document:

https://python-socketio.readthedocs.io/en/latest/.

Also tried version compatibility:

https://pypi.org/project/python-socketio/

pip3 freeze

I have tried multiple version for python-engineio and python-socketio to match my socket.io-client but no improvements.

eventlet==0.33.0
python-engineio==3.13.0
python-socketio==4.6.1

server.py

import eventlet
import socketio

sio = socketio.Server()
app = socketio.WSGIApp(sio)

@sio.event
def connect(sid, environ):
    print('[INFO] Connect to client', sid)

@sio.event
def disconnect(sid):
    print('disconnect ', sid)

#ESI.local 192.168.2.95
if __name__ == '__main__':
    eventlet.wsgi.server(eventlet.listen(('0.0.0.0', 5100)), app)

   

HTML/JavaScript

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<h1>Socket IO Demo</h1>
    <script type="module">
        import { io } from "https://cdn.socket.io/4.3.2/socket.io.esm.min.js";
        
        const sio = io("http://localhost:5100:", {
            forceJSONP: true,
            secure: true,
            jsonp: true,
            transports: [ "websocket","polling"]
        });

        sio.on("connect", () => {
            console.log("connected");
        });

        sio.on("disconnect", () => {
            console.log("Disconnected");
        });
    </script>
</body>
</html>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

After many trial and error. I have finally cracked it. This was happening due to CORS even though I used CORS google extension to handle this situation, but I guess this was supposed to be handled on backend. Simply passing (cors_allowed_origins="*") to socketio.Server() it fixed everything.

sio = socketio.Server(cors_allowed_origins="*")
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to perform websocket, i'll recommend using WebSocket package built-in and websocket from gevent.

Here what I have done to achieve a thing similar as what you want to do :

let socket = new WebSocket('ws://localhost:8080'); 
console.log('Created socket');

// Connection opened
socket.addEventListener('open', function (event) {
    socket.send('Hello Server!');
});

// Listen for messages
socket.addEventListener('message', function (event) {
    console.log('Message from server ', event.data);
});

// For example : send array
socket.send(JSON.stringify([1, 2, 3]));

Python server:

from geventwebsocket import WebSocketServer, WebSocketApplication, Resource
from collections import OrderedDict

class Application(WebSocketApplication):
    def on_open(self):
        print("Connection opened")

    def on_message(self, message):
        print("Got message: ", message)
        # self.ws.send(...) to make a response

    def on_close(self, reason):
        print(reason)

WebSocketServer(
    ('localhost', 8080),
    Resource(OrderedDict([('/', Application)]))
).serve_forever()

Gevent (python) : https://pypi.org/project/gevent-websocket/

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda