Quart错误的请求语法或不受支持的方法

时间:2019-06-19 07:39:08

标签: python quart asgi

Windows上的Python 3.7

运行sample from quart

from quart import Quart, websocket

app = Quart(__name__)

@app.route('/')
async def hello():
    return 'hello'

@app.websocket('/ws')
async def ws():
    while True:
        await websocket.send('hello')

app.run()

运行http://127.0.0.1:5000/ws时得到

Bad Request
Bad request syntax or unsupported method

1 个答案:

答案 0 :(得分:1)

您需要一个JS客户端来测试WebSocket,而不仅仅是浏览器,例如test-ws.html:

<!DOCTYPE html>
<html>
<body>
    <script>
    let socket = new WebSocket("ws://localhost:5000/ws");

    socket.onmessage = function(event) {
        alert(`Data received: ${event.data}`);
        socket.close();
    };
    </script>
</body>
</html>

(使用python3 -m http.server并转到http://127.0.0.1:8000/test-ws.html在浏览器中进行测试)