1、定义路由app.py
from flask import Flask, request from flask import Blueprint app = Flask(__name__) test = Blueprint('test', __name__) @app.before_request def before_request(): print("before_request") print(request) @app.after_request def after_request(param): print("after_request") print(param.data) # resp =app.make_response('after') return param @app.teardown_request def teardown_request(ex): print("teardown_request")
2、定义main入口main.py
from app import app from app import test import testController from flask import Flask, request if __name__ == '__main__': # 通过蓝图实现请求上下文 app.register_blueprint(test, url_prefix='/test') # 默认监听端口是5000 # 一定要指定debug=False不然断点不命中 app.run(host='0.0.0.0', port=888, debug=False)
3、定义控制器testController.py
from app import app from app import test @app.route('/hello', methods=['POST']) def hello(): return 'hello app!' @test.route('/hello', methods=['POST']) def hello(): return 'hello test!'