首页 > 解决方案 > python-socketio socketio.exceptions.ConnectionError:一个或多个命名空间无法连接

问题描述

我正在尝试连接到 socket.io 服务器并接收消息。但是在连接时我得到 socketio.exceptions.ConnectionError: One or more namespaces failed to connect

代码:

import socketio

sio = socketio.Client()

@sio.event
def connect():
    print('connection established')
    sio.emit('login', {'token': 'token'})

@sio.event
def my_message(data):
    print('message received with ', data)

@sio.event
def disconnect():
    print('disconnected from server')

sio.connect('wss://socket.boticord.top')
sio.wait()

完整的错误日志:

Attempting polling connection to https://socket.boticord.top/socket.io/?transport=polling&EIO=4
Polling connection accepted with {'sid': 'JBtkTO-XTOL-OFo2AAAH', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 5000}
Engine.IO connection established
Sending packet MESSAGE data 0
Attempting WebSocket upgrade to wss://socket.boticord.top/socket.io/?transport=websocket&EIO=4
WebSocket upgrade failed: connection error
Sending polling GET request to https://socket.boticord.top/socket.io/?transport=polling&EIO=4&sid=JBtkTO-XTOL-OFo2AAAH
Unexpected status code 400 in server response, aborting
Waiting for write loop task to end
Sending packet CLOSE data None
Engine.IO connection dropped
Traceback (most recent call last):
  File "c:\Users\Kiril\Documents\senko\bot2\senkobot\main2.py", line 6, in <module>
    sio.connect('wss://socket.boticord.top')
  File "C:\Users\Kiril\AppData\Local\Programs\Python\Python39\lib\site-packages\socketio\client.py", line 338, in connect
    raise exceptions.ConnectionError(
socketio.exceptions.ConnectionError: One or more namespaces failed to connect
HTTP POST request to https://socket.boticord.top/socket.io/?transport=polling&EIO=4&sid=JBtkTO-XTOL-OFo2AAAH failed with error HTTPSConnectionPool(host='socket.boticord.top', port=443): Read timed out. (read timeout=5).
Connection refused by the server, aborting
Exiting write loop task
Exiting read loop task

标签: pythonwebsocketsocket.iopython-socketio

解决方案


尝试

sio.connect('wss://socket.boticord.top', wait_timeout = 10)

推荐阅读