问题描述:
最近使用宝塔7.7一键安装脚本后,Centos7.9环境下,无法使用自带的ssh终端功能。点击后一直黑屏。并且提示:连接丢失,正在尝试重新连接!
尝试网上的方法都无法解决,尤之前大概一两年前都没有此问题,检查报错信息
查看日志有如下websocket错误日志
Traceback (most recent call last):
File "/www/server/panel/pyenv/lib/python3.7/site-packages/gevent/pywsgi.py", line 999, in handle_one_response
self.run_application()
File "/www/server/panel/pyenv/lib/python3.7/site-packages/geventwebsocket/handler.py", line 75, in run_application
self.run_websocket()
File "/www/server/panel/pyenv/lib/python3.7/site-packages/geventwebsocket/handler.py", line 52, in run_websocket
list(self.application(self.environ, lambda s, h, e=None: []))
File "/www/server/panel/pyenv/lib/python3.7/site-packages/flask/app.py", line 2552, in __call__
return self.wsgi_app(environ, start_response)
File "class/flask_sockets.py", line 30, in __call__
handler, values = adapter.match()
File "/www/server/panel/pyenv/lib/python3.7/site-packages/werkzeug/routing/map.py", line 622, in match
raise WebsocketMismatch() from None
werkzeug.routing.exceptions.WebsocketMismatch: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
2024-06-24T05:18:01Z {'REMOTE_ADDR': '', 'REMOTE_PORT': '39082', 'HTTP_HOST': '', (hidden keys: 31)} failed with WebsocketMismatch
问题所在:
在使用 Flask-Sockets 进行 WebSocket 连接时,会遇到如下错误信息:werkzeug.routing.WebsocketMismatch: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
这个问题的解决方法发现了两种,接下来分别对两种方法进行说明。
方法一:降低 Flask 和 Werkzeug 版本
这种错误有时是因为 Flask 版本过高,降级即可成功,但是这种方式可能不够理想,推荐查看方法二。
btpip install flask==1.1.2 btpip install Werkzeug==1.0.2
方法二(推荐)
需要对 flask_sockets 库的源代码进行手动修改。具体的修改如下:
修改文件:/www/server/panel/class/flask_sockets.py,第78行把
self.url_map.add(Rule(rule, endpoint=f))
修改为:
self.url_map.add(Rule(rule, endpoint=f, websocket=True))
参考链接:
https://github.com/heroku-python/flask-sockets/issues/81