1.中间件
# coding=utf-8
from flask import Flask
app = Flask(__name__)
app.config.from_object("settings.DevelopmentConfig")
@app.route("/index", methods=["POST", "GET"])
def index():
return "index"
class Middleware(object):
def __init__(self, wsgi_app):
"""服务启动时自动执行"""
self.old_wsgi_app = wsgi_app
def __call__(self, environ, start_response):
"""每次有用户请求时执行"""
print('before')
obj = self.old_wsgi_app(environ, start_response)
print("after")
return obj
if __name__ == '__main__':
app.wsgi_app = Middleware(app.wsgi_app)
app.run()