[Python] Python Socketio client has a transport error by connection with Flask-SocketIO-Server...

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    I want to connect my Raspberry Pi 5 whit a python-socketio client to connect whit my Flask-SocketIO server on a different device. I'm using a nginx as a WebSocket Reverse Proxy. My Problem is that i got regularly "transport error" and the client disconnected, without me doing anything or there being any connection problems. I checked the internet connection but I couldn't find any connection problems and could not detect any difference between a LAN and WLAN connection. The strange thing is, that when i use the same Code on my Windows PC it worked whitout a Problem. The Server was befor added the connect to my Raspberry Pi 5 only for iOS Apps and it worked also well. What could be the problem or what can I do to better identify the problem?

    Here is the Error on my Raspberry Pi 5:

    Received packet PING data
    [INFO ] Received packet PING data
    Sending packet PONG data
    [INFO ] Sending packet PONG data
    WebSocket connection was closed, aborting
    [WARNING] WebSocket connection was closed, aborting
    Waiting for write loop task to end
    [INFO ] Waiting for write loop task to end
    Exiting write loop task
    [INFO ] Exiting write loop task
    Engine.IO connection dropped
    [INFO ] Engine.IO connection dropped
    [DISCONNECT]: reason: transport error
    [DEBUG]: "transport error"
    Connection failed, new attempt in 3.30 seconds
    Exiting read loop task
    [INFO ] Connection failed, new attempt in 3.30 seconds
    [INFO ] Exiting read loop task
    Attempting WebSocket connection to wss://www.mylink.de/socket.io/?transport=websocket&EIO=4
    [INFO ] [Attempting WebSocket connection to wss]//www.mylink.de/socket.io/?transport=websocket&EIO=4
    WebSocket connection accepted with {'sid': 'dGtmnqUR9rU279m1AAAU', 'upgrades': [], 'pingTimeout': 20000, 'pingInterval': 25000, 'maxPayload': 1000000}
    [INFO ] [WebSocket connection accepted with {'sid'] 'dGtmnqUR9rU279m1AAAU', 'upgrades': [], 'pingTimeout': 20000, 'pingInterval': 25000, 'maxPayload': 1000000}
    Engine.IO connection established
    [INFO ] Engine.IO connection established
    Sending packet MESSAGE data 0{}
    [INFO ] Sending packet MESSAGE data 0{}
    Received packet MESSAGE data 0{"sid":"sVrC7eaUscRr558dAAAV"}
    [INFO ] Received packet MESSAGE data 0{"sid":"sVrC7eaUscRr558dAAAV"}
    Namespace / is connected
    [INFO ] Namespace / is connected
    [INFO]: Verbunden mit dem Server.
    Emitting event "test" [/]
    [INFO ] Emitting event "test" [/]
    Sending packet MESSAGE data 2["test",{"Hallo":"World"}]
    [INFO ] Sending packet MESSAGE data 2["test",{"Hallo":"World"}]
    Reconnection successful
    [INFO ] Reconnection successful


    Here is the Error on my Server:

    dkoIY3aUHWE7_HlkAAAS: Sending packet PING data None
    dkoIY3aUHWE7_HlkAAAS: Received packet PONG data
    dkoIY3aUHWE7_HlkAAAS: Sending packet PING data None
    dkoIY3aUHWE7_HlkAAAS: Received packet PONG data
    --------------------------------------------------------------------------------
    [DISCONNECT]reason = transport close
    [DISCONNECT] - traceback: NoneType: None
    --------------------------------------------------------------------------------
    dGtmnqUR9rU279m1AAAU: Sending packet OPEN data {'sid': 'dGtmnqUR9rU279m1AAAU', 'upgrades': [], 'pingTimeout': 20000, 'pingInterval': 25000, 'maxPayload': 1000000}
    dGtmnqUR9rU279m1AAAU: Received request to upgrade to websocket
    dGtmnqUR9rU279m1AAAU: Upgrade to websocket successful
    dGtmnqUR9rU279m1AAAU: Received packet MESSAGE data 0{}
    dGtmnqUR9rU279m1AAAU: Sending packet MESSAGE data 0{"sid":"sVrC7eaUscRr558dAAAV"}
    Ein Client hat sich verbunden.
    dGtmnqUR9rU279m1AAAU: Received packet MESSAGE data 2["test",{"Hallo":"World"}]
    received event "test" from sVrC7eaUscRr558dAAAV [/]
    dGtmnqUR9rU279m1AAAU: Sending packet PING data None
    dGtmnqUR9rU279m1AAAU: Received packet PONG data
    dGtmnqUR9rU279m1AAAU: Sending packet PING data None
    dGtmnqUR9rU279m1AAAU: Received packet PONG data
    dGtmnqUR9rU279m1AAAU: Sending packet PING data None
    --------------------------------------------------------------------------------
    [DISCONNECT]reason = transport close
    [DISCONNECT] - traceback: NoneType: None

    --------------------------------------------------------------------------------
    dGtmnqUR9rU279m1AAAU: Received packet PONG data
    gNo1jQEFB1pggf7-AAAW: Sending packet OPEN data {'sid': 'gNo1jQEFB1pggf7-AAAW', 'upgrades': [], 'pingTimeout': 20000, 'pingInterval': 25000, 'maxPayload': 1000000}
    gNo1jQEFB1pggf7-AAAW: Received request to upgrade to websocket
    gNo1jQEFB1pggf7-AAAW: Upgrade to websocket successful
    gNo1jQEFB1pggf7-AAAW: Received packet MESSAGE data 0{}
    gNo1jQEFB1pggf7-AAAW: Sending packet MESSAGE data 0{"sid":"p2sePiDTxpxpymyQAAAX"}
    Ein Client hat sich verbunden.
    gNo1jQEFB1pggf7-AAAW: Received packet MESSAGE data 2["test",{"Hallo":"World"}]
    received event "test" from p2sePiDTxpxpymyQAAAX [/]
    gNo1jQEFB1pggf7-AAAW: Sending packet PING data None
    gNo1jQEFB1pggf7-AAAW: Received packet PONG data


    First I have checked and updated the Version from Flask and flask_socketio on my server and on my client from python-socketio[client], then i have reduced the code of the server and client to a "connect" and "disconnect" function. Here is the Code from my client:

    def run_socketio_client(self):
    client = SocketIOClient()
    client.connect("https://www.mylink.de") # Server-URL
    client.wait()


    import socketio

    class SocketIOClient:
    def __init__(self):
    self.sio = socketio.Client(
    reconnection=True,
    reconnection_delay=3,
    reconnection_delay_max=10,
    logger=True,
    engineio_logger=True
    )

    # Events registrieren
    self.sio.on('connect', self.on_connect)
    self.sio.on('disconnect', self.on_disconnect)

    def on_connect(self):
    print("[INFO]: Verbunden mit dem Server.")
    self.sio.emit("test", {"Hallo": "World"})

    def on_disconnect(self, reason=None):
    print("[DISCONNECT]: reason:", reason)

    if reason == self.sio.reason.CLIENT_DISCONNECT:
    print("[DEBUG]: CLIENT_DISCONNECT")
    elif reason == self.sio.reason.SERVER_DISCONNECT:
    print("[DEBUG]: SERVER_DISCONNECT")
    elif reason == self.sio.reason.TRANSPORT_ERROR:
    print("[DEBUG]: \"transport error\"")
    else:
    print('[DEBUG]: other reason:', reason)

    def connect(self, url):
    print("[INFO]: Verbindungsaufbau mit dem Server...")
    self.sio.connect(
    url,
    transports=['websocket'],
    wait_timeout=10,
    retry=True
    )

    def wait(self):
    self.sio.wait()


    and here from my server:

    from flask import Flask, request
    from flask_socketio import SocketIO, emit, join_room, leave_room, close_room, rooms, disconnect
    import traceback

    app = Flask(__name__)
    socketio = SocketIO(app,
    server=True,
    logger=True,
    engineio_logger=True,
    cors_allowed_origins="*",
    async_mode="gevent",
    transports=["websocket"])

    @socketio.on_error_default
    def handle_error(e):
    print(f"[ERROR] - error: {e}")
    print("[ERROR] - handle_error traceback:")
    print(traceback.format_exc())

    # WebSocket: Verbindung hergestellt
    @socketio.on('connect')
    def handle_connect():
    print('Ein Client hat sich verbunden.')

    @socketio.on('disconnect')
    def disconnect(reason=None):
    #sid = request.sid
    print("------------------------------------------------------------------------------")
    print("[DISCONNECT]reason =", reason)
    print("[DISCONNECT] - traceback:", traceback.format_exc())
    print("------------------------------------------------------------------------------")


    if __name__ == '__main__':
    socketio.run(app, port=5000)


    Unfortunately the Raspberry still has regularly a transport error and my Windows program with the same code doesn't.

    Continue reading...

Compartilhe esta Página