当前位置:   article > 正文

【python-flask服务器跑APP完整版总结】flask + gevent-websocket + gunicorn +nginx + supervisor部署完整过程【各种细节总结】_服务器端flask跑起来

服务器端flask跑起来

声明:python的服务器建议使用gunicorn,而uwsgi服务器比较复杂,看个人情况来,个人不建议使用,太麻烦,使用uwsgi配置websocket反正我是一直没连通!!!

gunicorn 是同时支持http和websocket的访问

第一步:安装gunicorn服务器和测试http、websocket请求是否成功:

第一步参考文章:https://www.jianshu.com/p/69e75fc3e08e

Gunicorn是一个unix上被广泛使用的高性能的Python WSGI UNIX HTTP Server。
和大多数的web框架兼容,并具有实现简单,轻量级,高性能等特点。

gunicorn 安装

pip install gunicorn

gunicorn + flask 简单示例

python测试文件:testSockets.py

  1. from flask import Flask
  2. from flask_sockets import Sockets
  3. app = Flask(__name__)
  4. sockets = Sockets(app)
  5. @sockets.route('/')
  6. def echo_socket(ws):
  7. while not ws.closed:
  8. # 服务器端没关闭之前必须有接收阻塞,否则服务器和客户端都会死循环而崩溃
  9. # websocket的原理:客户端发送消息请求,服务器端回复数据【客户端不发消息,循环不会阻塞就是死循环】
  10. message = ws.receive()
  11. print("监听成功")
  12. ws.send(message)
  13. @app.route('/')
  14. def hello():
  15. return 'http请求访问成功'
  16. if __name__ == "__main__":
  17. from gevent import pywsgi
  18. from geventwebsocket.handler import WebSocketHandler
  19. server = pywsgi.WSGIServer(('0.0.0.0', 9099), app, handler_class=WebSocketHandler)
  20. server.serve_forever()

通过gunicorn运行flask app【兼容websocket的运行方式】

  1. // pip install geventwebsocket【首先必须安装geventwebsocket】
  2. (uniapp-Flask) (base) [root@VM_0_2_centos websocketTest]# gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker --bind 0.0.0.0:9099 testSockets:app
  3. [2019-07-29 15:57:54 +0800] [8932] [INFO] Starting gunicorn 19.9.0
  4. [2019-07-29 15:57:54 +0800] [8932] [INFO] Listening at: http://0.0.0.0:9099 (8932)
  5. [2019-07-29 15:57:54 +0800] [8932] [INFO] Using worker: geventwebsocket.gunicorn.workers.GeventWebSocketWorker
  6. [2019-07-29 15:57:54 +0800] [8939] [INFO] Booting worker with pid: 8939

测试http请求结果【非nginx的情况】

  1. # curl http://127.0.0.1:9099
  2. 'http请求访问成功'

测试websocket请求 【非nginx的情况】

  1. 测试ws请求
  2. 测试网站:http://www.blue-zero.com/WebSocket/
  3. ws://ip:9099

第二步:使用gunicorn的配置文件配置参数【官方建议使用py文件即可】

  1. # gunicorn.py服务器的配置文件,更多参数参考官方文档:http://docs.gunicorn.org/en/stable/configure.html
  2. import multiprocessing
  3. bind = "0.0.0.0:8099"
  4. workers = multiprocessing.cpu_count() * 2 + 1
  5. chdir="/root/uniapp-Flask"
  6. backlog = 2048
  7. worker_class = "geventwebsocket.gunicorn.workers.GeventWebSocketWorker"
  8. worker_connections = 1000
  9. daemon = True # 是否开启后台启动【True开启】
  10. debug = True
  11. # gunicorn -c ../gunicorn.py testSockets:app

第三步:nginx.conf 的核心配置

  1. # websocket配置参考官方文档:http://nginx.org/en/docs/http/websocket.html
  2. server {
  3. listen 80 default_server;
  4. server_name 0.0.0.0;
  5. location / {
  6. include uwsgi_params;
  7. proxy_pass http://119.28.180.116:9099;
  8. # 下面三个选项是websocket必须的【参考官方文档】
  9. proxy_http_version 1.1;
  10. proxy_set_header Upgrade $http_upgrade;
  11. proxy_set_header Connection "upgrade";
  12. }
  13. }

supervisor管理多进程【非常重要】

参考我的这篇文章:https://blog.csdn.net/weixin_43343144/article/details/97660632

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/280171
推荐阅读
相关标签
  

闽ICP备14008679号