Loading

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()
posted @ 2020-04-23 08:36  开花的马铃薯  阅读(143)  评论(0编辑  收藏  举报