python - Quart Bad Request 语法或不支持的方法
问题描述
Windows 上的 Python 3.7
从夸脱运行样品时
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
解决方案
您需要一个 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在浏览器中进行测试)
推荐阅读
- reactjs - Firebase 动态链接不适用于带有电容器的 ios
- hive - 如何在 2.2.0 以上版本的 hive 中开启隐式类型转换?
- javascript - 问题设置从 react-native-location 取消订阅到状态变量
- angular - Angular / .NET Core 项目上的 Cors 问题
- php - 从 MySQL 获取项目数组
- algorithm - 如何在 ECC 中创建构造函数点?(请帮助)
- angular - 如何使用离子打字稿解决 Firebase 未捕获的错误?
- asp.net - IIS下的VB自动实现属性
- javascript - jQuery 有时不会改变视频 src
- c++ - 从文本文件中读取单词:奇怪的行为