Python Flask 首次运行:请勿在生产环境中使用开发服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51025893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flask at first run: Do not use the development server in a production environment
提问by Anatoly
I installed the Flask plugin in PyCharm Community Edition and I just have this simple code in my flask app:
我在 PyCharm 社区版中安装了 Flask 插件,并且在我的 Flask 应用程序中只有这个简单的代码:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return '<h1>Hello!</h1>'
if __name__ == "__main__":
app.run(debug=True)
And I get this message:
我收到这条消息:
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead
* Restarting with stat
* Debugger is active!
* Debugger PIN: 123-456-789
* Running on http://127.0.0.1:5000/
Why am I getting this error when I run Flask?
为什么在运行 Flask 时会出现此错误?
A previous version of the message read "Do not use the development server in a production environment."
该消息的先前版本为“请勿在生产环境中使用开发服务器”。
采纳答案by davidism
Unless you tell the development server that it's running in development mode, it will assume you're using it in production and warn you not to. The development server is not intended for use in production. It is not designed to be particularly efficient, stable, or secure.
除非您告诉开发服务器它正在以开发模式运行,否则它会假设您正在生产中使用它并警告您不要这样做。开发服务器不适用于生产。它的设计目的不是特别高效、稳定或安全。
Enable development mode by setting the FLASK_ENV
environment variable to development
.
通过将FLASK_ENV
环境变量设置为development
.
export FLASK_ENV=development
flask run
If you're running in PyCharm (or probably any other IDE) you can set environment variables in the run configuration.
如果您在 PyCharm(或可能是任何其他 IDE)中运行,您可以在运行配置中设置环境变量。
Development mode enables the debugger and reloader by default. If you don't want these, pass --no-debugger
or --no-reloader
to the run
command.
开发模式默认启用调试器和重新加载器。如果您不想要这些,请传递--no-debugger
或传递--no-reloader
给run
命令。
That warning is just a warning though, it's not an error preventing your app from running. If your app isn't working, there's something else wrong with your code.
该警告只是一个警告,并不是阻止您的应用程序运行的错误。如果您的应用程序无法运行,则说明您的代码存在其他问题。
回答by Yuchen Zhong
The official tutorial discusses deploying an app to production.One option is to use Waitress, a production WSGI server. Other servers include Gunicorn and uWSGI.
官方教程讨论了将应用程序部署到生产环境。一种选择是使用 Waitress,一个生产 WSGI 服务器。其他服务器包括 Gunicorn 和 uWSGI。
When running publicly rather than in development, you should not use the built-in development server (
flask run
). The development server is provided by Werkzeug for convenience, but is not designed to be particularly efficient, stable, or secure.Instead, use a production WSGI server. For example, to use Waitress, first install it in the virtual environment:
$ pip install waitress
You need to tell Waitress about your application, but it doesn't use
FLASK_APP
like flask run does. You need to tell it to import and call the application factory to get an application object.$ waitress-serve --call 'flaskr:create_app' Serving on http://0.0.0.0:8080
在公开运行而不是在开发中运行时,不应使用内置开发服务器 (
flask run
)。开发服务器由 Werkzeug 提供以方便使用,但其设计目的并不是特别高效、稳定或安全。相反,使用生产 WSGI 服务器。比如要使用Waitress,首先要在虚拟环境中安装:
$ pip install waitress
您需要将您的应用程序告诉 Waitress,但它
FLASK_APP
不像 flask run 那样使用。您需要告诉它导入并调用应用程序工厂以获取应用程序对象。$ waitress-serve --call 'flaskr:create_app' Serving on http://0.0.0.0:8080
Or you can use waitress.serve()
in the code instead of using the CLI command.
或者您可以waitress.serve()
在代码中使用而不是使用 CLI 命令。
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "<h1>Hello!</h1>"
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=8080)
$ python hello.py