Django rest framework源码分析(3)----节流
2018-04-05 16:44
603 查看
目录
Django rest framework(1)----认证
Django rest framework(2)----权限
Django rest framework(3)----节流
Django rest framework(4)----版本
Django rest framework(5)----解析器
Django rest framework(6)----序列化
Django rest framework(7)----分页
限制60s内只能访问3次
(1)API文件夹下面新建throttle.py,代码如下:
(2)settings中全局配置节流
(3)现在访问auth看看结果:
60s内访问次数超过三次,会限制访问
提示剩余多少时间可以访问
utils/throttle.py
Django rest framework(1)----认证
Django rest framework(2)----权限
Django rest framework(3)----节流
Django rest framework(4)----版本
Django rest framework(5)----解析器
Django rest framework(6)----序列化
Django rest framework(7)----分页
添加节流
自定义节流的方法限制60s内只能访问3次
(1)API文件夹下面新建throttle.py,代码如下:
# utils/throttle.py from rest_framework.throttling import BaseThrottle import time VISIT_RECORD = {} #保存访问记录 class VisitThrottle(BaseThrottle): '''60s内只能访问3次''' def __init__(self): self.history = None #初始化访问记录 def allow_request(self,request,view): #获取用户ip (get_ident) remote_addr = self.get_ident(request) ctime = time.time() #如果当前IP不在访问记录里面,就添加到记录 if remote_addr not in VISIT_RECORD: VISIT_RECORD[remote_addr] = [ctime,] #键值对的形式保存 return True #True表示可以访问 #获取当前ip的历史访问记录 history = VISIT_RECORD.get(remote_addr) #初始化访问记录 self.history = history #如果有历史访问记录,并且最早一次的访问记录离当前时间超过60s,就删除最早的那个访问记录, #只要为True,就一直循环删除最早的一次访问记录 while history and history[-1] < ctime - 60: history.pop() #如果访问记录不超过三次,就把当前的访问记录插到第一个位置(pop删除最后一个) if len(history) < 3: history.insert(0,ctime) return True def wait(self): '''还需要等多久才能访问''' ctime = time.time() return 60 - (ctime - self.history[-1])
(2)settings中全局配置节流
#全局 REST_FRAMEWORK = { #节流 "DEFAULT_THROTTLE_CLASSES":['API.utils.throttle.VisitThrottle'], }
(3)现在访问auth看看结果:
60s内访问次数超过三次,会限制访问
提示剩余多少时间可以访问
# utils/throttle.py # # from rest_framework.throttling import BaseThrottle # import time # VISIT_RECORD = {} #保存访问记录 # # class VisitThrottle(BaseThrottle): # '''60s内只能访问3次''' # def __init__(self): # self.history = None #初始化访问记录 # # def allow_request(self,request,view): # #获取用户ip (get_ident) # remote_addr = self.get_ident(request) # ctime = time.time() # #如果当前IP不在访问记录里面,就添加到记录 # if remote_addr not in VISIT_RECORD: # VISIT_RECORD[remote_addr] = [ctime,] #键值对的形式保存 # return True #True表示可以访问 # #获取当前ip的历史访问记录 # history = VISIT_RECORD.get(remote_addr) # #初始化访问记录 # self.history = history # # #如果有历史访问记录,并且最早一次的访问记录离当前时间超过60s,就删除最早的那个访问记录, # #只要为True,就一直循环删除最早的一次访问记录 # while history and history[-1] < ctime - 60: # history.pop() # #如果访问记录不超过三次,就把当前的访问记录插到第一个位置(pop删除最后一个) # if len(history) < 3: # history.insert(0,ctime) # return True # # def wait(self): # '''还需要等多久才能访问''' # ctime = time.time() # return 60 - (ctime - self.history[-1]) from rest_framework.throttling import SimpleRateThrottle class VisitThrottle(SimpleRateThrottle): '''匿名用户60s只能访问三次(根据ip)''' scope = 'NBA' #这里面的值,自己随便定义,settings里面根据这个值配置Rate def get_cache_key(self, request, view): #通过ip限制节流 return self.get_ident(request) class UserThrottle(SimpleRateThrottle): '''登录用户60s可以访问10次''' scope = 'NBAUser' #这里面的值,自己随便定义,settings里面根据这个值配置Rate def get_cache_key(self, request, view): return request.user.username
utils/throttle.py
相关文章推荐
- Django Rest Framework----ModelViewSet视图 ModelViewSet源码分析
- Django rest framework源码分析(4)----版本
- Django rest framework源码分析(1)----认证
- django Rest Framework----APIView 执行流程 APIView 源码分析
- Django rest framework 权限操作(源码分析)
- Django rest framework 的认证流程(源码分析)
- django1.8源码分析 -- WSGI是如何启动的
- Django源码分析之server
- Django源码分析--1
- django.contrib.auth之authenticate函数源码分析
- Django Signals 从实践到源码分析(转)
- Django-Rest-Framework部分源码流程分析
- Django——基于类的视图源码分析 三
- django源码分析记载的博客
- Django源码分析之权限系统
- Django源码分析--引导
- Django Rest Framework源码剖析(一)-----认证
- 深入学习Django源码基础16 - django中信号的学习分析
- Django——基于类的视图源码分析 二
- django自动重新加载模块 源码分析