钩子函数

2022-09-14 18:46:00 浏览数 (1)

一、概述

类似于Django中的中间件,使用需要用对应的钩子装饰器的函数

二、App对象使用

  • before_first_request 第一次请求之前
    • 参数:没有参数
    • 返回值 None 请求可以继续进行 response对象 终止本次请求,直接返回
  • before_request 每次请求之前
    • 参数:没有参数
    • 返回值 None 请求可以继续进行 response对象 终止本次请求,直接返回
  • after_request
    • 参数:视图中创建的response对象
    • 返回值 response对象 可以在返回前修改响应
  • teardown_request 每次请求之后,即使有异常发生 需要将debug关闭
    • 参数:exception错误信息
    • 返回值 None 请求可以继续进行

实例

代码语言:javascript复制
<span class="hljs-meta">@app.before_first_request</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">beforeFirstRequest</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">"----beforeFirstRequest----"</span>)
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'index.html'</span>)
<span class="hljs-meta">@app.before_request</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">beforeRequest</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">"-------------before"</span>)
    <span class="hljs-comment"># 验证验证</span>
    <span class="hljs-comment"># 获取前端发送的验证码</span>
    <span class="hljs-comment"># 获取session中的验证码</span>
    <span class="hljs-comment"># 判断两者是否相同,相同返回None,否则重定向会登陆界面</span>

    <span class="hljs-comment"># 验证是否登录</span>
    <span class="hljs-comment"># 获取状态保持相关信息(账号)</span>
    <span class="hljs-comment"># 如果没有状态保持说明没有登陆,重定向登陆界面</span>
    <span class="hljs-comment"># 根据状态保持的账号获取用户对象</span>
    <span class="hljs-comment"># 获取客户端发送的cookie中的键为token的值</span>
    <span class="hljs-comment"># 如果没有说明没有登陆,重定向登陆界面</span>
    <span class="hljs-comment"># 判断用户中的token与cookie中的token值是否相同,不相同则重定向登陆界面</span>

三、蓝本对象中使用

参数与返回值同上

钩子函数

功能描述

before_app_first_request

第一次请求之前

before_app_request

每次请求之前

after_app_request

每次请求之后,前提是没有异常

teardown_app_request

每次请求之后,即使有异常发生 需要将debug关闭

实例

代码语言:javascript复制
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> g,request, Blueprint

hook = Blueprint(<span class="hljs-string">'hook'</span>, __name__)

<span class="hljs-meta">@hook.route("/gouzi/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hook</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">"----hook----"</span>)
    <span class="hljs-keyword">return</span> <span class="hljs-string">"gouzi"</span>

<span class="hljs-meta">@hook.before_app_first_request</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">beforeFirstRequest</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">"----beforeFirstRequest----"</span>)
<span class="hljs-meta">@hook.before_app_request</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">beforeRequest</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">if</span> request.method == <span class="hljs-string">'GET'</span> <span class="hljs-keyword">and</span> request.path == <span class="hljs-string">'/form/'</span>:
        abort(<span class="hljs-number">500</span>) <span class="hljs-comment">#如果传递方式为get 并且请求的是 form的路由 则禁止请求</span>
    <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> request.headers.get(<span class="hljs-string">'User-Agent'</span>) <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span> request.host == <span class="hljs-string">'127.0.0.1:5000'</span>:
        abort(<span class="hljs-number">500</span>)
    print(<span class="hljs-string">"----beforeRequest----"</span>)
    g.a = <span class="hljs-string">"good man"</span>
<span class="hljs-meta">@hook.after_app_request</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">afterRequest</span><span class="hljs-params">(response)</span>:</span>
    print(<span class="hljs-string">"----afterRequest----"</span>)
    print(g.a)
    <span class="hljs-keyword">return</span> response
<span class="hljs-meta">@hook.teardown_app_request</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">teardownRequest</span><span class="hljs-params">(exception)</span>:</span>
    print(<span class="hljs-string">"----teardownRequest----"</span>)
    print(exception)

四、仿照Django中的中间件

应用目录下创建middlewares包目录,每个功能实现以个中间件(每个功能是一个py文件)

在middlewares目录下创建中间件文件

代码语言:javascript复制
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> request
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> session
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> g

<span class="hljs-comment">#参数:蓝图对象</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">verifycodeMiddleware</span><span class="hljs-params">(blueprint)</span>:</span>
<span class="hljs-meta">    @blueprint.before_app_request</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">before</span><span class="hljs-params">()</span>:</span>
        print(<span class="hljs-string">"------验证验证码-------"</span>)

注册中间件(蓝图文件中)

代码语言:javascript复制
<span class="hljs-comment"># 注册中间件</span>
<span class="hljs-keyword">from</span> myApp.middlewares <span class="hljs-keyword">import</span> verifycodeMiddleware
verifycodeMiddleware(myApp)

如果卸载app.py中代码如下

代码语言:javascript复制
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> request
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> session
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> g

<span class="hljs-comment">#参数:蓝图对象</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">verifycodeMiddleware</span><span class="hljs-params">(app)</span>:</span>
<span class="hljs-meta">    @app.before_request</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">before</span><span class="hljs-params">()</span>:</span>
        print(<span class="hljs-string">"****验证验证码****"</span>)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create_app</span><span class="hljs-params">()</span>:</span>
  verifycodeMiddleware(app)

0 人点赞