詳解django自定義中間件處理
中間件是一個(gè)鉤子框架,它們可以介入 Django 的請(qǐng)求和響應(yīng)處理過程。 它是一個(gè)輕量級(jí)、底層的 插件 系統(tǒng),用于在 全局修改 Django 的輸入或輸出 。
每個(gè)中間件組件負(fù)責(zé)完成某個(gè)特定的功能
這里介紹的中間件方法適用于 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ǔ)中間件源碼,要習(xí)慣于看源碼,上面的這段代碼并不復(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__ 為每個(gè)請(qǐng)求/響應(yīng)執(zhí)行的代碼
self.process_request(request) 為每個(gè)請(qǐng)求到調(diào)用視圖之前的操作,通??梢栽谶@里做一些用戶請(qǐng)求頻率的控制。
self.get_response(request) 為調(diào)用視圖
self.process_response(request, response) 為調(diào)用視圖完成后的操作
自定義中間件
剛才了解了基礎(chǔ)中間件,現(xiàn)在就開始編寫我們自己的中間件。
通常我們回去繼承基礎(chǔ)中間件來實(shí)現(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
如果你要在請(qǐng)求之前做處理,需要定義 process_request() 方法,去實(shí)現(xiàn)相關(guān)功能
如果你要在視圖調(diào)用之后做處理,需要定義 process_response() 方法,去實(shí)現(xiàn)相關(guān)功能
:warning:注意 定義 process_response() 方法一定要 return response
需要將你編寫的中間件添加到 settings 中的 MIDDLEWARE 里
我這里寫了一個(gè)通過中間件限制客戶端請(qǐng)求頻率,有興趣的可以看一下
django中間件客戶端請(qǐng)求頻率限制
通過redis lua腳本對(duì)客戶端IP請(qǐng)求頻率限制
# 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中間件客戶端請(qǐng)求頻率限制
"""
limit = 4 # 單位時(shí)間內(nèi)允許請(qǐng)求次數(shù)
expire = 1 # 限制時(shí)間
cache = "default" # 獲取django cache
def process_request(self, request):
num = self.set_key(request)
if num > self.limit:
return HttpResponse("請(qǐng)求頻率過快,請(qǐng)稍后重試", 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è)置請(qǐng)求請(qǐng)求次數(shù)和限制時(shí)間
:param request:
:return: 限制時(shí)間內(nèi)請(qǐng)求次數(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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
opencv 圖像腐蝕和圖像膨脹的實(shí)現(xiàn)
這篇文章主要介紹了opencv 圖像腐蝕和圖像膨脹的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
使用Python進(jìn)行二進(jìn)制文件讀寫的簡(jiǎn)單方法(推薦)
下面小編就為大家?guī)硪黄褂肞ython進(jìn)行二進(jìn)制文件讀寫的簡(jiǎn)單方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
python實(shí)現(xiàn)將range()函數(shù)生成的數(shù)字存儲(chǔ)在一個(gè)列表中
這篇文章主要介紹了python實(shí)現(xiàn)將range()函數(shù)生成的數(shù)字存儲(chǔ)在一個(gè)列表中,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
numpy.ndarray 交換多維數(shù)組(矩陣)的行/列方法
今天小編就為大家分享一篇numpy.ndarray 交換多維數(shù)組(矩陣)的行/列方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Pandas處理DataFrame稀疏數(shù)據(jù)及維度不匹配數(shù)據(jù)分析詳解
這篇文章主要為大家介紹了Pandas處理DataFrame稀疏數(shù)據(jù)及維度不匹配數(shù)據(jù)分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
解決PyCharm不在run輸出運(yùn)行結(jié)果而不是再Console里輸出的問題
這篇文章主要介紹了解決PyCharm不在run輸出運(yùn)行結(jié)果而不是再Console里輸出的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

