python - flask_socketio使用uswgi启动访问socketio会400错误

 aaaaaa师太_667 发布于 2022-10-26 13:37

python依赖:

  1. flask

  2. flask_socketio

  3. uwsgi

  4. gevent

/web/__init__.py

from flask import Flask
from flask_socketio import SocketIO, emit
app = Flask(__name__)
socket_io = SocketIO(app)

@socket_io.on('test')
def handle_my_custom_event(json):
    print(json)
    emit('my response', json)

/web/static/index.html


/run.sh

#!/bin/sh
uwsgi --http :8000 --gevent 1000 --http-websockets --master --module web --callable app

然后访问http://127.0.0.1:8000/static/index.html。抓包400错误无法访问socket.io

上的使用方法来自flask_socketio文档。其中下面的使用nginx代理的方式也用过了。没有用一样是400。

上面的uwsgi只能在linux上用。。我全部都是在docker中做的测试。

1 个回答
  • uwsgi 不是了解。
    我用的是Gunicorn,也遇到相同问题。
    查看文档后使用自定义gevent web server解决了。

    When using gunicorn with the gevent worker and the WebSocket support provided by gevent-websocket, the command that starts the server must be changed to select a custom gevent web server that supports the WebSocket protocol. The modified command is:
    gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 module:app

    pip install gevent-websocket

    2022-11-12 01:39 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有