我在PyCharm社区版中安装了烧瓶插件,我在烧瓶应用程序中只有这个简单的代码:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return '<h1>Hello!</h1>'
if __name__ == "__main__":
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)
我得到这个消息:
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/
Run Code Online (Sandbox Code Playgroud)
当我来到http://127.0.0.1:5000时,它说:"在服务器上找不到请求的URL.如果你手动输入了URL,请检查你的拼写,然后再试一次."
Yuc*_*ong 28
取决于你想做什么.如果你确实想在生产中制作它,你应该检查一下:
http://flask.pocoo.org/docs/dev/tutorial/deploy/
在公开而不是在开发中运行时,不应使用内置开发服务器(烧瓶运行).开发服务器由Werkzeug提供以方便使用,但其设计不是特别高效,稳定或安全.
而是使用生产WSGI服务器.例如,要使用Waitress,请先在虚拟环境中安装它:
在你的情况下,你可以试试这样:
$ pip install waitress
Run Code Online (Sandbox Code Playgroud)
dav*_*ism 27
除非你告诉开发服务器它正在开发模式下运行,否则它会假设你在生产中使用它并警告你不要.
通过将FLASK_ENV环境变量设置为启用开发模式development.
export FLASK_ENV=development
flask run
Run Code Online (Sandbox Code Playgroud)
如果您在PyCharm(或可能是任何其他IDE)中运行,则可以在运行配置中设置环境变量.
开发模式默认启用调试器和重新加载器.如果您不想要这些,请传递--no-debugger或--no-reloader执行run命令.
小智 16
尝试 gevent:
from flask import Flask
from gevent.pywsgi import WSGIServer
app = Flask(__name__)
@app.route('/api', methods=['GET'])
def index():
return "Hello, World!"
if __name__ == '__main__':
# Debug/Development
# app.run(debug=True, host="0.0.0.0", port="5000")
# Production
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
Run Code Online (Sandbox Code Playgroud)
注意:使用 pip install gevent 安装 gevent