• Django中间件


    中间件

     

    一、url请求经过中间件的执行顺序

    中间件描述起来可能会很麻烦,一张图片更能简洁明了的django中间件的大概作用。

     

    django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法。

    在django项目的settings模块中,有一个 MIDDLEWARE_CLASSES 变量,其中每一个元素就是一个中间件,如下图。

    每一个中间件中都可以定义四个方法,分别是:

    • process_request(self,request)
    • process_view(self, request, callback, callback_args, callback_kwargs)
    • process_exception(self, request, exception)
    • process_response(self, request, response)

    以上方法的返回值可以是None和HttpResonse对象,如果是None,则继续按照django定义的规则向下执行,如果是HttpResonse对象,则直接将该对象返回给用户。

     

    二、自定义中间件

    1、创建放置中间件的目录和文件

     

    2、自定义中间件

    class mmm(object):
    
        def process_request(self, request):
            print 'mmm.process_request'
    
        def process_view(self, request, callback, callback_args, callback_kwargs):
            print 'mmm.process_view'
        def process_response(self, request, response):
    
            print 'mmm.process_response'
            return response
    
    class xxx(object):
    
        def process_request(self, request):
            print 'xxx.process_request'
    
        def process_view(self, request, callback, callback_args, callback_kwargs):
            print 'xxx.process_view'
        def process_response(self, request, response):
    
            print 'xxx.process_response'
            return response

     

    3、在settings.py里注册自定义的中间件类

    MIDDLEWARE_CLASSES = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        'middleware.middle1.mmm',
        'middleware.middle1.xxx',
    ]

     

    在执行时,可以看到请求在经过这中间件时的执行顺序

     

     

     

     只有当程序执行错误时,才会执行process_exception(self, request, exception)

    可以在views里的函数里触发一个错误,然后执行程序。

    三、django中间件源码剖析

    中间件的源码可以再wsgi.py里找到

    点击进入到WSGIHandler类:

    在WSGIHandler类的里面的 _call_方法里面会执行self.load_middleware()

    在load_middleware方法里,定义了5个空列表。此方法会去循环settings.py下的MIDDLEWARE_CLASSES列表中所有中间件的类名。然后,在通过hasattr反射的方法去查找所有的中间件类里面是否有对应的process_requestprocess_viewprocess_responseprocess_exception,如果有这些方法就会把他们分别一一添加到定义的五个空列表里,然后再去循环四个列表,并分别去执行列表下的函数

     1 def load_middleware(self):
     2         """
     3         Populate middleware lists from settings.MIDDLEWARE_CLASSES.
     4 
     5         Must be called after the environment is fixed (see __call__ in subclasses).
     6         """
     7         self._view_middleware = []
     8         self._template_response_middleware = []
     9         self._response_middleware = []
    10         self._exception_middleware = []
    11 
    12         request_middleware = []
    13         for middleware_path in settings.MIDDLEWARE_CLASSES:
    14             mw_class = import_string(middleware_path)
    15             try:
    16                 mw_instance = mw_class()
    17             except MiddlewareNotUsed as exc:
    18                 if settings.DEBUG:
    19                     if six.text_type(exc):
    20                         logger.debug('MiddlewareNotUsed(%r): %s', middleware_path, exc)
    21                     else:
    22                         logger.debug('MiddlewareNotUsed: %r', middleware_path)
    23                 continue
    24 
    25             if hasattr(mw_instance, 'process_request'):
    26                 request_middleware.append(mw_instance.process_request)
    27             if hasattr(mw_instance, 'process_view'):
    28                 self._view_middleware.append(mw_instance.process_view)
    29             if hasattr(mw_instance, 'process_template_response'):
    30                 self._template_response_middleware.insert(0, mw_instance.process_template_response)
    31             if hasattr(mw_instance, 'process_response'):
    32                 self._response_middleware.insert(0, mw_instance.process_response)
    33             if hasattr(mw_instance, 'process_exception'):
    34                 self._exception_middleware.insert(0, mw_instance.process_exception)
    35 
    36         # We only assign to this when initialization is complete as it is used
    37         # as a flag for initialization being complete.
    38         self._request_middleware = request_middleware

     

  • 相关阅读:
    记一次失败的发布经历
    使用Test Director 编写测试用例的一点经验
    Windows Embedded 7 Standard 裁剪过程图解教程
    人生中第一次刮餐饮发票中奖5块!
    Proj EULibHarn Paper Reading: IntelliGen: Automatic Driver Synthesis for FuzzTesting
    Proj EULibHarn Paper Reading: FuzzGen: Automatic Fuzzer Generation
    Proj THUDBFuzz Paper Reading: Lenient Execution of C on a Java Virtual Machine
    Proj AutoWriter Paper Reading: Abstract Syntax Networks for Code Generation and Semantic Parsing
    Proj THUDBFuzz Paper Reading: ContextAware FailureOblivious Computing as a Means of Preventing Buffer Overflows
    Proj EULibHarn Paper Reading: FUDGE: Fuzz Driver Generation at Scale
  • 原文地址:https://www.cnblogs.com/chenchao1990/p/5280423.html
Copyright © 2020-2023  润新知