234.10.flask-中间件

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()

posted @ 2022-06-02 13:54  楠海  阅读(22)  评论(0编辑  收藏  举报