Windows下运行Flask-SocketIO应用遇连接错误,寻求技术协助
Flask-SocketIO 在 Windows 下连接失败,Linux 运行正常
问题概述
同一份 Flask-SocketIO 代码在 Ubuntu 上运行无问题,迁移到 Windows 10/11 后客户端抛出 socketio.exceptions.ConnectionError: One or more namespaces failed to connect 错误,但 HTTP 层面访问 localhost:5000 可正常返回响应。
代码片段
服务器代码
from flask import Flask from flask_socketio import SocketIO app = Flask(__name__) app.config['SECRET_KEY'] = 'secret_key' socketio = SocketIO(app=app, logging='DEBUG') @socketio.on_error_default def error_handler(e): print('An error occurred:', e) @app.route('/') # 注意:原代码遗漏了装饰器的@符号,已修正 def index(): return 'Hello, Socket.IO Client!' if __name__ == '__main__': socketio.run(app, port=5000)
客户端代码
import socketio import logging sio = socketio.Client() logging.basicConfig(level=logging.DEBUG) @sio.event def connect(): print('Connected to server') @sio.event def disconnect(): print('Disconnected from server') sio.connect('http://localhost:5000')
环境信息
- Python 版本:3.9.13
- 依赖包版本:
- Flask==2.0.1
- flask-restplus==0.13.0
- Flask-SocketIO==5.1.0
- python-engineio==4.2.0
- python-socketio==5.3.0
已排查项
- Windows 10、11 均测试,问题一致
- 关闭防火墙、更换端口、管理员权限运行、改用
flask run启动均无效 - Windows 主机的 Linux Docker 容器内运行正常
- 未修改网络设置或端口转发
日志信息
客户端完整日志
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): localhost:5000 DEBUG:urllib3.connectionpool:http://localhost:5000 "GET /socket.io/?transport=polling&EIO=4&t=1688031616.8050513 HTTP/1.1" 200 None websocket-client package not installed, only polling transport is available ERROR:engineio.client:websocket-client package not installed, only polling transport is available DEBUG:urllib3.connectionpool:Resetting dropped connection: localhost DEBUG:urllib3.connectionpool:Starting new HTTP connection (2): localhost:5000 Traceback (most recent call last): File "C:\Users\Matt\source\repos\data-art\hg-backend\Client.py", line 15, in <module> sio.connect('http://localhost:5000') File "C:\Users\Matt\source\repos\data-art\hg-backend\venv\lib\site-packages\socketio\client.py", line 323, in connect raise exceptions.ConnectionError( socketio.exceptions.ConnectionError: One or more namespaces failed to connect DEBUG:urllib3.connectionpool:http://localhost:5000 "POST /socket.io/?transport=polling&EIO=4&sid=XDn3Nhf3Wlaepe5fAAAA HTTP/1.1" 200 None DEBUG:urllib3.connectionpool:http://localhost:5000 "GET /socket.io/?transport=polling&EIO=4&sid=XDn3Nhf3Wlaepe5fAAAA&t=1688031618.8611357 HTTP/1.1" 200 None Connected to server
服务器完整日志
* Serving Flask app 'Server' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 127.0.0.1 - - [29/Jun/2023 10:40:18] "GET /socket.io/?transport=polling&EIO=4&t=1688031616.8050513 HTTP/1.1" 200 - 127.0.0.1 - - [29/Jun/2023 10:40:20] "POST /socket.io/?transport=polling&EIO=4&sid=XDn3Nhf3Wlaepe5fAAAA HTTP/1.1" 200 - 127.0.0.1 - - [29/Jun/2023 10:40:20] "GET /socket.io/?transport=polling&EIO=4&sid=XDn3Nhf3Wlaepe5fAAAA&t=1688031618.8611357 HTTP/1.1" 200 - 127.0.0.1 - - [29/Jun/2023 10:41:20] "GET /socket.io/?EIO=4&transport=polling HTTP/1.1" 200 -
解决方法
修复服务器路由装饰器:原代码中
app.route('/')遗漏了@符号,修正为@app.route('/'),确保路由正常注册。安装 websocket-client 依赖:客户端日志提示缺少 websocket 支持,仅能使用轮询传输,Windows 下轮询易出现连接重置问题,安装依赖切换到更稳定的传输方式:
pip install websocket-client强制使用 polling 传输并延长超时:若安装 websocket 后仍有问题,强制客户端使用 polling 并延长超时时间,避免误判连接失败:
sio.connect('http://localhost:5000', transports=['polling'], wait_timeout=10)改用 IPv4 地址连接:Windows 下
localhost可能优先解析到 IPv6,而服务器默认绑定 IPv4 的127.0.0.1,修改客户端连接地址:sio.connect('http://127.0.0.1:5000')更新依赖包版本:当前依赖版本较旧,Windows 下可能存在已修复的兼容性问题,更新到稳定版本:
pip install --upgrade Flask Flask-SocketIO python-engineio python-socketio
内容的提问来源于stack exchange,提问作者Matt Barker
相关产品推荐
相关产品推荐

