1 . flask的中间件

  1)@app.before_request     # 请求进入视图函数之前,类似于django中间件的request_process

  2)@app.after_request      # 响应返回客户端之前,类似于django中间件的response_process

  manage.py 代码如下 :

 from flask import Flask

  app = Flask(__name__)

  @app.before_request
  def be1():
    print('我是be1')   @app.before_request
  def be2():
    print('我是be2')   @app.before_request
  def be3():
    print('我是be3')   @app.after_request
  def af1(response):
    print('我是af1')
    return response   @app.after_request
  def af2(response):
    print('我是af2')
    return response   @app.after_request
  def af3(response):
    print('我是af3')
    return response   @app.route('/index')
  def index():
    print('我是视图函数')
    return 'ok'   if __name__ == '__main__':
    app.run(debug=True)

  启动项目, 访问 http://127.0.0.1:5000/index,打印结果如下 :

  我是be1
  我是be2
  我是be3
  我是视图函数
  我是af3
  我是af2
  我是af1

  分析:@app.after_request自上而下依次执行,@app.after_request自下而上依次执行。与django的中间件类似,@app.after_request装饰的函数不写return或者returnNone表示放行,顺利通过,否则拦截请求,返回响应。@app.after_request装饰的函数一定要有返回值,且必须有参数接收response对象。

  修改be1函数为如下代码:

  @app.before_request
  def be2():
    print('我是be2')
    return 'error'

  执行结构如下 :

  我是be1
  我是af3
  我是af2
  我是af1

注意

它与 Django的中间件还是有区别的,

正常情况下 : be1 - be2 - be3 - af3 - af2 - af1

异常情况下 : be1 - be2- af3 - af2 - af1

在Flask的特殊装饰器 before_request 和 after_request 中,不管请求进入试图函数之前会走到哪一步,在响应的时候都要从做开始做响应.

2 . 重定义错误页面返回信息

  @app.errorhandler(404)# 参数是错误代码
  def error404(error_info):# 注意,一定要加参数接收错误信息
    print(error_info)
    # 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
    return '页面不存在'# 可以返回 三剑客 + 小儿子

  当访问发生404错误时,会看到该函数的返回内容.

before-request , after-request的更多相关文章

  1. String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";作用!!!!!

    <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+re ...

  2. C#中 Request, Request.params , Request.querystring , Request.Form 区别 与联系用法

    C#中 Request, Request.params , Request.querystring , Request.Form 区别 与联系用法? Request.params , Request ...

  3. basePath = request.getScheme()+"://"+request.getServerName()+":"+r

    basePath = request.getScheme()+"://"+request.getServerName()+":"+r (2014-06-30 1 ...

  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+pat----------<base>元素有关

    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. ...

  5. request.get request.GET……

    发现他们是不同的. 报错: AttributeError at /add/ 'WSGIRequest' object has no attribute 'get' Request Method: GE ...

  6. 客户端的数据来源:QueryString, Form, Cookie Request[]与Request.Params[]

    在ASP.NET编程中,有三个比较常见的来自于客户端的数据来源:QueryString, Form, Cookie . 我们可以在HttpRequest中访问这三大对象. QueryString: 获 ...

  7. JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)

    本随笔这是作为一个记录使用,以备后查.项目完成之后本地部署OK,本地Linux部署OK,都可以正常的访问,可是当我把它部署到服务器上面的时候,首页可以正常访问,可是当发出请求的时候却报错误了,说找不到 ...

  8. 安卓开发笔记(十六):'Request(okhttp3.Request.Builder)' has private access in 'okhttp3.Request

    当出现了'Request(okhttp3.Request.Builder)' has private access in 'okhttp3.Request的错误的时候,实际上是我们在写代码的时候少打了 ...

  9. <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

    <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+re ...

  10. [转载]Request、Request.Form和Request.QueryString的区别

    Request.Request.Form和Request.QueryString的区别 request本身是一个系统的静态对象,本身也可以作为数组调用,比如request("abc" ...

随机推荐

  1. 2016.7.12 去除mybatis-generator生成的class里的注释

    用mybatis-generator自动生成代码会出现很多没必要的注释.     在配置文件里加上: 是否去除所有自动生成的文件的时间戳: 是否去除所有自动生成文件的注释: <commentGe ...

  2. [javase学习笔记]-8.5 statickeyword的使用场景

    这一节我们来看一下在我们开发的过程中,在什么时候我们要用到statickeyword进行静态修饰. 我们这里所说的静态.无非就是两种.一种是静态变量,一种是静态函数,我们分这两种情况进行说明stati ...

  3. F - 概率(经典问题)

    Description Sometimes some mathematical results are hard to believe. One of the common problems is t ...

  4. css:清除浮动 overflow

    是因为overflow除了(visible)会重新给他里面的元素建立块级格式化(block formatting context)floats, position absolute, inline-b ...

  5. ARM和STM32的区别及ARM公司架构的发展

    ARM和STM32的区别及ARM公司架构的发展 转:https://www.cnblogs.com/kwdeblog/p/5260348.html ARM是英国的芯片设计公司,其最成功的莫过于32位嵌 ...

  6. NativeBase自定义组件样式

    http://nativebase.io/docs/v0.5.13/customize#themingNativeBaseApp 对于NativeBase中的组件,我们可以根据实际需要来进行自定义组件 ...

  7. centos安装python3.7.0过程记录

    参考自这里,整理出以下步骤. 一.下载python3.7.0包地址:https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz 二.安装 登陆Li ...

  8. Web大文件(夹)上传(断点续传)控件-Xproer.HttpUploader6

    版权所有 2009-2017荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...

  9. 深入Asyncio(二)从线程到协程

    线程的真相 多线程并不是一无是处,在实际问题中,要权衡优劣势来选择多线程.多进程或是协程.协程为多线程的某些问题提供了一种解决方案,所以学习协程首先要对线程有一定了解. 多线程优点 代码可读性 多线程 ...

  10. Docker入门系列6 如何打开多个终端进入Docker容器

    Docker容器运行后,如何进入容器进行操作呢?起初我是用SSH.如果只启动一个容器,用SSH还能应付,只需要将容器的22端口映射到本机的一个端口即可.当我启动了五个容器后,每个容器默认是没有配置SS ...