Django中間件工作流程及寫法實例代碼
熟悉web開發(fā)的同學對hook鉤子肯定不陌生,通過鉤子可以方便的實現(xiàn)一些觸發(fā)和回調,并且做一些過濾和攔截。
django中的中間件(middleware)就是類似鉤子的一種存在。下面我們來介紹一下,并且給出一些實例。
1、Middleware的工作流程

我盜了一個圖,看網(wǎng)上很多人用這個圖,來源已經(jīng)追不明白了。簡單聲明一下,這個圖不是我的??粗鴪D我們分析一下:
1)django的請求相應流程:HttpRequest -> RequestMiddleware -> view function -> ResponseMiddleware -> HttpResponse
可以看到一個請求到響應的過程,中間夾著兩個middleware流程,請求中間件和響應中間件。
也就是說,django提供了一種機制,在:
- 請求到達視圖函數(shù)中間
- 視圖函數(shù)到響應之間
支持嵌入鉤子。
這種鉤子的特點:
- 全局,一旦你使用了中間件,并且發(fā)布生效的話,所有的請求都會經(jīng)過你嵌入的中間件。
- 性能敏感,如果你的中間件性能差的話,那么會影響服務的整體性能。
2) django的middleware包含四個鉤子函數(shù):process_request/process_view/process_response/process_exception
process_request:接受request之后確定所執(zhí)行的view之前
process_view:確定了所要執(zhí)行的view之后,view真正執(zhí)行之前
process_response:view執(zhí)行之后
process_exceptionview:view執(zhí)行拋出異常
而插入middleware的過程是在settings.py中配置,如下默認配置,我只截取了兩個中間件:SessionMiddleware和CommonMiddleware。
MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', ... )
我們簡單看一下SessionMiddleware的實現(xiàn)
import time
from importlib import import_module
from django.conf import settings
from django.utils.cache import patch_vary_headers
from django.utils.http import cookie_date
class SessionMiddleware(object):
def __init__(self):
engine = import_module(settings.SESSION_ENGINE)
self.SessionStore = engine.SessionStore
def process_request(self, request):
session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME, None)
request.session = self.SessionStore(session_key)
def process_response(self, request, response):
"""
If request.session was modified, or if the configuration is to save the
session every time, save the changes and set a session cookie or delete
the session cookie if the session has been emptied.
"""
try:
accessed = request.session.accessed
modified = request.session.modified
empty = request.session.is_empty()
except AttributeError:
pass
else:
# First check if we need to delete this cookie.
# The session should be deleted only if the session is entirely empty
if settings.SESSION_COOKIE_NAME in request.COOKIES and empty:
response.delete_cookie(settings.SESSION_COOKIE_NAME)
else:
if accessed:
patch_vary_headers(response, ('Cookie',))
if modified or settings.SESSION_SAVE_EVERY_REQUEST:
if request.session.get_expire_at_browser_close():
max_age = None
expires = None
else:
max_age = request.session.get_expiry_age()
expires_time = time.time() + max_age
expires = cookie_date(expires_time)
# Save the session data and refresh the client cookie.
# Skip session save for 500 responses, refs #3881.
if response.status_code != 500:
request.session.save()
response.set_cookie(settings.SESSION_COOKIE_NAME,
request.session.session_key, max_age=max_age,
expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
path=settings.SESSION_COOKIE_PATH,
secure=settings.SESSION_COOKIE_SECURE or None,
httponly=settings.SESSION_COOKIE_HTTPONLY or None)
return response
可以看到在SessionMiddleware中默認只實現(xiàn)了process_request和process_response兩個hook函數(shù)。
我們就以這個例子說明一下一個請求的執(zhí)行過程。我們假設場景是這樣的:
1)從上而下配置了兩個Middleware(注意順序):SessionMiddleware和CommonMiddleware
2)每個Middleware中的四個鉤子函數(shù)齊全process_request/process_view/process_response/process_exception
執(zhí)行順序應該是這樣的:
1、HttpRequest
2、SessionMiddleware process_request
3、SessionMiddleware process_view
4、CommonMiddleware process_request
5、CommonMiddleware process_view
6、view
7、CommonMiddleware process_response
8、CommonMiddleware process_exception(如有必要)
9、SessionMiddleware process_response
10、SessionMiddleware process_exception(如有必要)
11、HttpResponse
2、Middleware的寫法
Middleware的寫法很簡單:
1)實現(xiàn)一個類,繼承object就行;
2)重寫其中的四個鉤子函數(shù)就可以了。
這里要著重說一個常用的功能。
攔截器/過濾器(filter)
一般來說,每一個請求都要經(jīng)過process_request這個鉤子函數(shù)。你的實現(xiàn)中,函數(shù)的執(zhí)行結果必然有兩種(你要自己做異常處理):
1)None
2)HttpResponse 對象
如果返回None,請求流程繼續(xù)執(zhí)行,也就是繼續(xù)進入其他的Middleware或者鉤子函數(shù)。
如果返回HttpResponse對象,那么就直接返回到頁面。通過這個功能我們可以做黑名單。
給一個例子:
就是統(tǒng)計pv
# -*- coding:utf-8 -*-
from datetime import datetime
from data_monitor.utils.dbmanager import MysqlManager
from data_monitor.common.constant import MYSQL_JOBS as mysql_config
class RequestHookMiddleware(object):
def process_request(self, request):
try:
username = request.COOKIES.get('username')
uri = request.path
timestamp = str(datetime.now())
db_obj = MysqlManager(
mysql_config.get('host'),
mysql_config.get('port'),
mysql_config.get('db'),
mysql_config.get('user'),
mysql_config.get('password'),
format=True,
)
field_str = 'username, uri, timestamp'
value_str = '"%s","%s","%s"' % (username, uri, timestamp)
db_obj.insert('pv', field_str, value_str)
db_obj.close()
return
except Exception, ex:
return
總結
以上就是本文關于Django中間件工作流程及寫法實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
Python?torch.fft.rfft()函數(shù)用法示例代碼
大家應該都知道新舊版的torch中的傅里葉變換函數(shù)在定義和用法上有所不同,下面這篇文章主要給大家介紹了關于Python?torch.fft.rfft()函數(shù)用法的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-04-04
獨立進程使用django模型及django.setup()使用
這篇文章主要介紹了獨立進程使用django模型(django.setup()使用),它提供了一種簡單且高效的方式來利用Django強大的功能,并使你的代碼更易于維護和擴展,需要的朋友可以參考下2023-07-07

