您的位置:首页 > 编程语言 > Go语言

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,代码如下:

# 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: