您当前的位置: 首页 >  ar

Xavier Jiezou

暂无认证

  • 2浏览

    0关注

    394博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

WARNING: Do not use the development server in a production environment. Use a production WSGI server

Xavier Jiezou 发布时间:2021-08-03 16:01:09 ,浏览量:2

文章目录
  • 问题描述
  • 原因分析
  • 解决方案
    • 方案一
    • 方案二
      • wsgiref.simple_server😀
      • waitress😀
      • Gunicorn😐
      • uWSGI😣
      • Gevent🙂
      • TwistedWeb😣
  • 温馨提示
  • 引用参考

问题描述

开发了一个 Python Flask Web 项目:

from flask import Flask
import waitress

app = Flask(__name__)


@app.route("/")
def hello_world():
    return "

Hello, World!

" if __name__ == '__main__': app.run(debug=True, host='127.0.0.1', port='5000')

运行时打印警告:

 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 146-032-183
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
原因分析

Flask 的默认服务器是在开发环境中使用的,仅供开发测试。Flask 配置环境默认为生产环境,所以才会报该错误。如果项目开发完成,想投入生产,那么就要换一个生产级的 WSGI 服务器了。

解决方案 方案一

若项目尚处于开发阶段,请将环境设置为开发环境,可以解决报错。

from flask import Flask
import waitress

app = Flask(__name__)
app.config['ENV'] = 'development'


@app.route("/")
def hello_world():
    return "

Hello, World!

" if __name__ == '__main__': app.run(debug=True, host='127.0.0.1', port='5000')
方案二

若项目已开发完成,想投入生产环境。可以选择 WSGI 服务器。

  • wsgiref.simple_server
  • waitress
  • Gunicorn
  • uWSGI
  • Gevent
  • Twisted Web
wsgiref.simple_server😀

实现了一个简单的 HTTP 服务器(基于 http.server),为 WSGI 应用程序提供服务。

from flask import Flask
from wsgiref.simple_server import make_server

app = Flask(__name__)


@app.route("/")
def hello_world():
    return "

Hello, World!

" if __name__ == '__main__': http_server = make_server('127.0.0.1', 5000, app) http_server.serve_forever()
waitress😀

生产级的纯 Python WSGI 服务器,具有非常可观的性能。

pip install waitress
from flask import Flask
import waitress # pip install waitress

app = Flask(__name__)


@app.route("/")
def hello_world():
    return "

Hello, World!

" if __name__ == '__main__': waitress.serve(app, host='127.0.0.1', port='5000')

服务器运行不会有任何提示,浏览器地址栏输入 http://127.0.0.1:5000/ 即可查看结果。

Gunicorn😐

Windows 上使用各种报错,推荐在 Linux 上使用。

uWSGI😣

这个也很复杂,跳过!

Gevent🙂

基于协程的 Python 网络库,它使用 greenlet 在 libev 或 libuv 事件循环之上提供高级同步 API。

pip install gevent
from flask import Flask
from gevent.pywsgi import WSGIServer  # pip install gevent

app = Flask(__name__)


@app.route("/")
def hello_world():
    return "

Hello, World!

" if __name__ == '__main__': http_server = WSGIServer(('127.0.0.1', 5000), app) http_server.serve_forever()
TwistedWeb😣

看着就挺复杂的,不看了。

温馨提示

Flask 的开发服务器是 Werkzeug 设计的,便于测试,但不是那么的高效、稳定或安全。所以想投入生产环境,还是首选 WSGI 服务器。

引用参考

https://flask.palletsprojects.com/en/2.0.x/tutorial/deploy/#run-with-a-production-server https://docs.python.org/3/library/wsgiref.html#module-wsgiref.simple_server https://docs.pylonsproject.org/projects/waitress/en/latest/index.html https://gunicorn.org/ https://uwsgi-docs.readthedocs.io/en/latest/ http://www.gevent.org/ https://twistedmatrix.com/trac/wiki/TwistedWeb

关注
打赏
1661408149
查看更多评论
立即登录/注册

微信扫码登录

0.0478s