詳解django自定義中間件處理
中間件是一個鉤子框架,它們可以介入 Django 的請求和響應(yīng)處理過程。 它是一個輕量級、底層的 插件 系統(tǒng),用于在 全局修改 Django 的輸入或輸出 。
每個中間件組件負責完成某個特定的功能
這里介紹的中間件方法適用于 Django1.10 以上
相關(guān)文件: django middleware
Django基礎(chǔ)中間件
django.utils.deprecation.py class MiddlewareMixin(object): def __init__(self, get_response=None): self.get_response = get_response super(MiddlewareMixin, self).__init__() def __call__(self, request): response = None if hasattr(self, 'process_request'): response = self.process_request(request) if not response: response = self.get_response(request) if hasattr(self, 'process_response'): response = self.process_response(request, response) return response
以上為Django基礎(chǔ)中間件源碼,要習慣于看源碼,上面的這段代碼并不復(fù)雜,下面我們來一一解釋。
def __init__(self, get_response=None): self.get_response = get_response super(MiddlewareMixin, self).__init__()
熟悉 python 類的都不陌生 __init__ 方法, 這里主要是 一次性配置和初始化
def __call__(self, request): response = None if hasattr(self, 'process_request'): response = self.process_request(request) if not response: response = self.get_response(request) if hasattr(self, 'process_response'): response = self.process_response(request, response) return response
__call__
為每個請求/響應(yīng)執(zhí)行的代碼
self.process_request(request)
為每個請求到調(diào)用視圖之前的操作,通常可以在這里做一些用戶請求頻率的控制。
self.get_response(request)
為調(diào)用視圖
self.process_response(request, response)
為調(diào)用視圖完成后的操作
自定義中間件
剛才了解了基礎(chǔ)中間件,現(xiàn)在就開始編寫我們自己的中間件。
通常我們回去繼承基礎(chǔ)中間件來實現(xiàn)自己的功能
from django.utils.deprecation import MiddlewareMixin class PermissionMiddlewareMixin(MiddlewareMixin): """ django 中間件 """ def process_request(self, request): pass def process_response(self, request, response): return response
如果你要在請求之前做處理,需要定義 process_request() 方法,去實現(xiàn)相關(guān)功能
如果你要在視圖調(diào)用之后做處理,需要定義 process_response() 方法,去實現(xiàn)相關(guān)功能
:warning:注意 定義 process_response() 方法一定要 return response
需要將你編寫的中間件添加到 settings 中的 MIDDLEWARE 里
我這里寫了一個通過中間件限制客戶端請求頻率,有興趣的可以看一下
django中間件客戶端請求頻率限制
通過redis lua腳本對客戶端IP請求頻率限制
# coding:utf-8 __author__ = 'carey@akhack.com' from django.utils.deprecation import MiddlewareMixin from django.http.response import HttpResponse from django_redis import get_redis_connection from hashlib import md5 class RequestBlockMiddlewareMixin(MiddlewareMixin): """ django中間件客戶端請求頻率限制 """ limit = 4 # 單位時間內(nèi)允許請求次數(shù) expire = 1 # 限制時間 cache = "default" # 獲取django cache def process_request(self, request): num = self.set_key(request) if num > self.limit: return HttpResponse("請求頻率過快,請稍后重試", status=503) @staticmethod def get_ident(request): """ Identify the machine making the request by parsing HTTP_X_FORWARDED_FOR if present and number of proxies is > 0. If not use all of HTTP_X_FORWARDED_FOR if it is available, if not use REMOTE_ADDR. """ NUM_PROXIES = 1 xff = request.META.get('HTTP_X_FORWARDED_FOR') remote_addr = request.META.get('REMOTE_ADDR') num_proxies = NUM_PROXIES if num_proxies is not None: if num_proxies == 0 or xff is None: return remote_addr addrs = xff.split(',') client_addr = addrs[-min(num_proxies, len(addrs))] return client_addr.strip() return ''.join(xff.split()) if xff else remote_addr def get_md5(self, request): """ 獲取IP md5值 :param request: :return: """ ip_str = self.get_ident(request) ip_md5 = md5() ip_md5.update(ip_str.encode("utf-8")) return ip_md5.hexdigest() def set_key(self, request): """ 通過redis lua腳本設(shè)置請求請求次數(shù)和限制時間 :param request: :return: 限制時間內(nèi)請求次數(shù) """ lua = """ local current current = redis.call("incr",KEYS[1]) if tonumber(current) == 1 then redis.call("expire",KEYS[1],ARGV[1]) end return tonumber(redis.call("get", KEYS[1])) """ key = self.get_md5(request) redis_cli = get_redis_connection(self.cache) data = redis_cli.eval(lua, 1, key, self.expire, self.limit) return data
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
matplotlib.pyplot畫圖并導(dǎo)出保存的實例
今天小編就為大家分享一篇matplotlib.pyplot畫圖并導(dǎo)出保存的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12使用pandas把某一列的字符值轉(zhuǎn)換為數(shù)字的實例
今天小編就為大家分享一篇使用pandas把某一列的字符值轉(zhuǎn)換為數(shù)字的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python+wxPython實現(xiàn)自動生成PPTX文檔程序
這篇文章主要介紹了如何使用 wxPython 模塊和 python-pptx 模塊來編寫一個程序,用于生成包含首頁、內(nèi)容頁和感謝頁的 PPTX 文檔,感興趣的小伙伴可以學(xué)習一下2023-08-08用Python按時間分割txt文件中的數(shù)據(jù)方法步驟
這篇文章主要給大家介紹了如何用Python按時間分割txt文件中的數(shù)據(jù)的方法步驟,文中通過代碼示例給大家講解的非常詳細,對大家學(xué)習Python處理txt文件有一定的幫助,需要的朋友可以參考下2023-12-12