flask的中间件
中间件,利用类的继承,重写__call__
的方法
##了解的知识点
from flask import Flask
app = Flask(__name__)
class MyMiddleware:
def __init__(self,old_wsgi_app):
self.old_wsgi_app =old_wsgi_app
def __call__(self, environ, start_response):
#这befor的befor
print("开始之前")
ret = self.old_wsgi_app(environ, start_response)
#这是after的after
print("结束之后")
return ret
@app.route("/")
def index():
return "ok"
if __name__ == '__main__':
app.wsgi_app = MyMiddleware(app.wsgi_app)
app.run()