flask之特殊装饰器(异常响应、类似于django中间件)

#! /usr/bin/python3
# -*- coding:utf-8 -*-
# file: lflask.py
# author: wangchenxi
# mail: wongchenxi@icloud.com
# brief:
# version: 0.1.00
# Create Time:2019-12-23 00:45:20
# Last Update: 2019-12-23 19时46分04秒

from flask import Flask
from flask import Blueprint

app = Flask(__name__)  # 配置传参后续章节再说明
user_bp = Blueprint("蓝图标识", __name__, url_prefix='/bp')


@user_bp.route("/user_bp")
def user():
    return "user_bp"


app.register_blueprint(user_bp)


@app.before_request
def be1():
    print("I am Be1")
    # return "返回Be1"


@app.before_request
def be2():
    print("I am Be2")
    # return "你废了"


@app.before_request
def be3():
    print("I am Be3")


@app.after_request
def af7086(resp):
    print("I am A1")
    return resp


@app.after_request
def af3721(resp):
    print("I am A2")
    return resp


@app.after_request
def af9527(resp):
    print("I am A3")
    return resp


@app.errorhandler(404)
def error404(errorMessage):
    print(errorMessage)
    return "当前页面不存在正在跳转.....10s后自动跳转"  # Flask 5种响应


if __name__ == '__main__':
    app.run('0.0.0.0', 5000)

版权声明:除特别注明外,本站所有文章均为王晨曦个人站点原创

转载请注明:出处来自王晨曦个人站点 » flask之特殊装饰器(异常响应、类似于django中间件)

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注