解决flask-socketIO 服务器部署 400 Bad Request 问题

简单说一下我出现的问题,测试时是在本地进行测试的,整个运行是很流畅的,也可以正常的推送内容,但是部署到服务器后就出现来大量的报错,也就是400 Bad Request 的错误

我采取的启动方式是 gunicorn,当然如果大家使用Nginx部署应该可以避免这个问题,这里只对我出现的状况进行解释:

查阅官方文档:https://flask-socketio.readthedocs.io/en/latest/#gunicorn-web-server

以下为官方文档内容:

Gunicorn Web Server
An alternative to socketio.run(app) is to use gunicorn as web server, using the eventlet or gevent workers. For this option, eventlet or gevent need to be installed, in addition to gunicorn. The command line that starts the eventlet server via gunicorn is:
gunicorn --worker-class eventlet -w 1 module:app
If you prefer to use gevent, the command to start the server is:
gunicorn -k gevent -w 1 module:app
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
In all these commands, module is the Python module or package that defines the application instance, and app is the application instance itself.
Due to the limited load balancing algorithm used by gunicorn, it is not possible to use more than one worker process when using this web server. For that reason, all the examples above include the -w 1 option.

注意点:

-w的参数,当前启动方式只支持 -w 1
多个worker的启动配置,则会出现HTTP 请求 400Bad Request的报错

当前的解决方法(我使用的是gevent):

1、安装gevent

pip install gevent

2、安装greenlet,greenlet是有版本要求的,直接下载最新版是没有问题
如果版本太低是可能直接导致项目启动不了,这是我遇到过的一个问题

pip install greenlet

修改启动命令:
注意:由于gunicorn使用的负载平衡算法有限,因此在使用此Web服务器时不能使用多个工作进程,只支持 -w 1

# 在未加入flask-socketIO时启动命令是:
gunicorn -w 4 -b 0.0.0.0:80 cms_app:app
# 加入flask-socketIO后启动命令是:
gunicorn -k gevent cms_app:app  -w 1 -b 0.0.0.0:80

成功启动,并正常运行

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值