Flask 是一个轻量级的 WSGI Web 应用程序框架。它旨在使快速入门 Web 开发变得容易,并具有扩展到复杂应用程序的能力。它最初是围绕 Werkzeug 和 Jinja 的简单包装,现在已成为最受欢迎的 Python Web 应用程序框架之一。
什么是 WSGI
WSGI 的全名是 Web Server Gateway Interface,Web 服务器网络接口,是为 Python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接口。重点在通用。简单理解就是 Python 的 Web 框架有很多,需要搭建一个通用网关,这样不论什么框架写的 Web 程序都能统一运行。毕竟统一和规范才能进步。
(FlaskGuide) ➜ FlaskGuide flask run * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
(FlaskGuide) ➜ FlaskGuide flask run * Environment: development * Debug mode: on * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 169-605-104
positional arguments: {shell,runserver} shell Runs a Python shell inside Flask application context. runserver Runs the Flask development server i.e. app.run()
optional arguments: -?, --help show this help message and exit
如果想运行程序,执行 python app.py runserver 命令。
使用 flask-script 自定义命令
举个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from flask import Flask from flask_script import Manager, Command
app = Flask(__name__) manager = Manager(app)
classHello(Command):
defrun(self): print("自定义命令测试")
if __name__ == '__main__': manager.add_command('hello', Hello()) manager.run()