# coding:utf-8 from flask import Flask app = Flask(__name__) @app.route("/index") def index(): print("index 執行了") return 'index page' @app.before_first_request def before_first_request_function(): """在第一次請求處理以前先被執行""" print("before first request function 執行了") @app.before_request def before_request_function(): """在每次請求以前都被執行""" print("before request function 執行了") @app.after_request def after_request_function(response): """在每次請求(視圖函數處理)以後都被執行, 前提是視圖函數沒有出現異常""" print("after request function 執行了") return response @app.teardown_request def teardown_request_function(response): """在每次請求 (視圖函數處理)以後都被執行, 不管視圖函數是否出現異常,都被執行, 工做在非調試模式時 debug=False""" print("teardown request function 執行了") if __name__ == '__main__': app.run(host='0.0.0.0')
# 展現結果 (Flask_py) python@python-VirtualBox:~/code$ python hook_demo.py * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) before first request function 執行了 before request function 執行了 index 執行了 after request function 執行了 teardown request function 執行了 127.0.0.1 - - [24/Jul/2019 19:39:20] "GET /index HTTP/1.1" 200 -
pip install Flask-Script
添加啓動腳本Managerpython
# coding:utf-8 from flask import Flask from flask_script import Manager # 啓動命令的管理類 app = Flask(__name__) # 建立Manager管理類的對象 manager = Manager(app) @app.route("/index") def index(): return "index page" if __name__ == '__main__': # app.run(debug=True) # 經過管理對象來啓動flask manager.run()
(Flask_py) python@python-VirtualBox:~/code$ python flask_scirpt_demo.py --help usage: flask_scirpt_demo.py [-?] {shell,runserver} ... 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
能夠看出有兩個命令可供咱們選擇,選定runserver再次經過help來查看幫助信息shell
(Flask_py) python@python-VirtualBox:~/code$ python flask_scirpt_demo.py runserver --help usage: flask_scirpt_demo.py runserver [-?] [-h HOST] [-p PORT] [--threaded] [--processes PROCESSES] [--passthrough-errors] [-d] [-D] [-r] [-R] Runs the Flask development server i.e. app.run() optional arguments: -?, --help show this help message and exit -h HOST, --host HOST -p PORT, --port PORT --threaded --processes PROCESSES --passthrough-errors -d, --debug enable the Werkzeug debugger (DO NOT use in production code) -D, --no-debug disable the Werkzeug debugger -r, --reload monitor Python files for changes (not 100{'const': True, 'help': 'monitor Python files for changes (not 100% safe for production use)', 'option_strings': ['-r', '--reload'], 'dest': 'use_reloader', 'required': False, 'nargs': 0, 'choices': None, 'default': None, 'prog': 'flask_scirpt_demo.py runserver', 'container': <argparse._ArgumentGroup object at 0x7f2a3e8db290>, 'type': None, 'metavar': None}afe for production use) -R, --no-reload do not monitor Python files for changes
(Flask_py) python@python-VirtualBox:~/code$ python flask_scirpt_demo.py runserver -h 0.0.0.0 -p 12345 * Running on http://0.0.0.0:12345/ (Press CTRL+C to quit) 127.0.0.1 - - [24/Jul/2019 20:00:43] "GET /index HTTP/1.1" 200 - 127.0.0.1 - - [24/Jul/2019 20:00:43] "GET /favicon.ico HTTP/1.1" 404 -
就是進入了ipython這個編譯器在這裏你不須要導入flask_script_demo這個模塊就能夠直接是用它flask