zoukankan      html  css  js  c++  java
  • 自定义中间件

    一、自定义中间件

    """
    @author RansySun
    @create 2019-12-16-19:08
    """
    from flask import Flask, flash, get_flashed_messages, request
    
    app = Flask(__name__)
    
    
    class MyMiddleware:
        """
        实现自定义中间件
    
        """
        def __init__(self, wsgi_app123):
            self.wsgi_app123 = wsgi_app123
    
        def __call__(self, environ, start_response):
            print("request请求进来之前,请求来定义中间件")
            res = self.wsgi_app123(environ, start_response)
            print("response之后返回页面,请求走时定义中间件")
            print(res)
            return res
        	"""
        	结果:
        	request请求进来之前
    		response之后返回页面
    		<werkzeug.wsgi.ClosingIterator object at 0x03D81850>
        	"""
    
    	@app.route('/index')
    	def index():
        	# request.method
        	# session['sd']
    
        	return "中间件实现"
    if __name__ == '__main__':
        app.wsgi_app = MyMiddleware(app.wsgi_app)
        # app.__call__()
        app.run()
    
    

    总结:

    1. 实现自定义中间在执行在app执行前和执行后执行,查看源码可以知道

      # app.__call__()
      
          def __call__(self, environ, start_response):
              """The WSGI server calls the Flask application object as the
              WSGI application. This calls :meth:`wsgi_app` which can be
              wrapped to applying middleware."""
              return self.wsgi_app(environ, start_response)
           def wsgi_app(self, environ, start_response):
                  """The actual WSGI application. This is not implemented in
              :meth:`__call__` so that middlewares can be applied without
              losing a reference to the app object. Instead of doing this::
      
                  app = MyMiddleware(app)
      
              It's a better idea to do this instead::
      
                  app.wsgi_app = MyMiddleware(app.wsgi_app)
      
              Then you still have the original application object around and
              can continue to call methods on it.
      
              .. versionchanged:: 0.7
                  Teardown events for the request and app contexts are called
                  even if an unhandled error occurs. Other events may not be
                  called depending on when an error occurs during dispatch.
                  See :ref:`callbacks-and-errors`.
      
              :param environ: A WSGI environment.
              :param start_response: A callable accepting a status code,
                  a list of headers, and an optional exception context to
                  start the response.
              """
                  pa
      
      
  • 相关阅读:
    CentOS7.4 + Ambari 2.6.1.5 + HDP 2.6.4.0 安装部署
    分布式业务的异常解决思路
    RPC簡介
    网络I/O模型--07Netty基础
    网络I/O模型--06异步I/O
    网络I/O模型--05多路复用I/O
    网络I/O模型--04非阻塞模式(解除accept()、 read()方法阻塞)的基础上加入多线程技术
    网络I/O模型--03非阻塞模式(ServerSocket与Socket的超时处理)--解除accept()、 read()方法阻塞
    网络I/O模型--02阻塞模式(多线程)
    Android开发(五)——计时器
  • 原文地址:https://www.cnblogs.com/randysun/p/15518205.html
Copyright © 2011-2022 走看看