Flask 中间件

Flask中间件

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的之前执行
        print("开始之前")
        ret = self.old_wsgi_app(environ, start_response)
        #这在after的之后执行
        print("结束之后")
        return ret

@app.route("/")
def index():
    return "ok"


if __name__ == '__main__':
    app.wsgi_app = MyMiddleware(app.wsgi_app)
    app.run()

 

posted @ 2020-03-19 21:12  Hank·Paul  阅读(155)  评论(0编辑  收藏  举报