Flask 首次运行:请勿在生产环境中使用开发服务器 [英] Flask at first run: Do not use the development server in a production environment

查看:76
本文介绍了Flask 首次运行:请勿在生产环境中使用开发服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PyCharm 社区版中安装了 Flask 插件,而我的 Flask 应用程序中只有这个简单的代码:

I installed the Flask plugin in PyCharm Community Edition and I just have this simple code in my flask app:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return '<h1>Hello!</h1>'

if __name__ == "__main__":
    app.run(debug=True)

我收到这条消息:

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/

为什么我在运行 Flask 时会收到此错误?

Why am I getting this error when I run Flask?

之前版本的消息是不要在生产环境中使用开发服务器."

推荐答案

除非您告诉开发服务器它正在以开发模式运行,否则它会假定您正在生产中使用它并警告您不要这样做.开发服务器不适用于生产.它的设计目的不是特别高效、稳定或安全.

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.

通过将 FLASK_ENV 环境变量设置为 development 来启用开发模式.

Enable development mode by setting the FLASK_ENV environment variable to development.

$ export FLASK_APP=example
$ export FLASK_ENV=development
$ flask run

如果您在 PyCharm(或可能是任何其他 IDE)中运行,您可以在运行配置中设置环境变量.

If you're running in PyCharm (or probably any other IDE) you can set environment variables in the run configuration.

开发模式默认启用调试器和重新加载器.如果您不想要这些,请将 --no-debugger--no-reloader 传递给 run 命令.

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.

该警告只是一个警告,并不是阻止您的应用运行的错误.如果您的应用无法运行,则说明您的代码存在其他问题.

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.

这篇关于Flask 首次运行:请勿在生产环境中使用开发服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆