-
-
Notifications
You must be signed in to change notification settings - Fork 16.6k
Closed
Description
New version of Flask (>1.1.4) does not work with Flask-Sockets (websockets).
Replicate bug by running server and client.
requirements:
Flask==2.0.2
Flask-Sockets==0.2.1
Werkzeug==2.0.2
gevent==21.8.0
websocket-client==1.2.1
flash_ws_server.py:
from flask import Flask
from flask_sockets import Sockets
app = Flask(__name__)
sockets = Sockets(app)
@sockets.route('/mixed_ws_path')
def echo_socket(ws):
while not ws.closed:
message = ws.receive()
ws.send(message)
@app.route('/mixed_ws_path')
def hello():
return 'Hello World!'
if __name__ == "__main__":
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
server = pywsgi.WSGIServer(('', 5000), app, handler_class=WebSocketHandler)
server.serve_forever()
flask_ws_client.py:
import websocket
import _thread
import time
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws, close_status_code, close_msg):
print("### closed ###")
def on_open(ws):
def run(*args):
for i in range(3):
time.sleep(1)
ws.send("Hello %d" % i)
time.sleep(1)
ws.close()
print("thread terminating...")
_thread.start_new_thread(run, ())
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://localhost:5000/mixed_ws_path",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()
The server responds with this exception:
Traceback (most recent call last):
File "C:\Users\heh\AppData\Local\Programs\Python\Python39\lib\site-packages\gevent\pywsgi.py", line 999, in handle_one_response
self.run_application()
File "C:\Users\heh\AppData\Local\Programs\Python\Python39\lib\site-packages\geventwebsocket\handler.py", line 75, in run_application
self.run_websocket()
File "C:\Users\heh\AppData\Local\Programs\Python\Python39\lib\site-packages\geventwebsocket\handler.py", line 52, in run_websocket
list(self.application(self.environ, lambda s, h, e=None: []))
File "C:\Users\heh\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 2091, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\heh\AppData\Local\Programs\Python\Python39\lib\site-packages\flask_sockets.py", line 40, in __call__
handler, values = adapter.match()
File "C:\Users\heh\AppData\Local\Programs\Python\Python39\lib\site-packages\werkzeug\routing.py", line 2039, in match
raise WebsocketMismatch()
werkzeug.routing.WebsocketMismatch: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
2021-10-08T09:59:35Z {'REMOTE_ADDR': '::1', 'REMOTE_PORT': '56214', 'HTTP_HOST': 'localhost:5000', (hidden keys: 24)} failed with WebsocketMismatch
Everything works fine with Flask version ==1.1.4
Environment:
- Python version: 3.9.0
- Flask version: 2.0.2
Metadata
Metadata
Assignees
Labels
No labels