1、节流自定义类:

import time
from api import models VISIT_RECORD = {} class VisitThrottle(BaseThrottle):
#设置访问频率为60秒3次
def allow_request(self, request, view):
#获取用户ID
# remote_addr = request.META.get('REMOTE_ADDR')
remote_addr = self.get_ident(request)
ctime = time.time()
if remote_addr not in VISIT_RECORD:
VISIT_RECORD[remote_addr] = [ctime]
history = VISIT_RECORD.get(remote_addr)
self.history = history
while history and history[-1] < ctime-60:
history.pop() if len(history) < 3:
history.insert(0,ctime)
return True def wait(self):
ctime = time.time()
wtime = 60 - (ctime-self.history[-1])
return wtime

2、内置节流类
a.项目下utils文件throttle.py文件:

from rest_framework.throttling import SimpleRateThrottle

#对匿名用户的限制
class VisitThrottle(SimpleRateThrottle):
scope = 'none' #限制规则写在settings全局配置里
def get_cache_key(self, request, view):
return self.get_ident(request) #对注册用户的限制
class UserThrottle(SimpleRateThrottle):
scope = 'user'
def get_cache_key(self, request, view):
return request.user.username

b.settings配置用户全局认证如下:

'DEFAULT_THROTTLE_CLASSES':['api.utils.throttle.VisitThrottle',], #节流认证
'DEFAULT_THROTTLE_RATES':{
'none':'3/m',
'user':'10/m',
},

c.views业务类可以在全局认证外设置单独认证规则:

throttle_classes = []

「Django」rest_framework学习系列-节流控制的更多相关文章

  1. 「Django」rest_framework学习系列-API访问跨域问题

    #以中间件方式解决API数据访问跨域问题1.API下新建文件夹下写PY文件a.引入内置类继承: from django.middleware.common import MiddlewareMixin ...

  2. 「Django」rest_framework学习系列-路由

    自动生成4个url路由:from rest_framework import routersrouter = routers.DefaultRouter()router.register(r'wrx' ...

  3. 「Django」rest_framework学习系列-渲染器

    渲染器:作用于页面,JSONRenderer只是JSON格式,BrowsableAPIRenderer有页面,.AdminRenderer页面以admin形式呈现(需要在请求地址后缀添加?fromat ...

  4. 「Django」rest_framework学习系列-分页

    分页a.分页,看第N页,每页显示N条数据方式一:使用PageNumberPagination创建分页对象,配合settings全局配置 views设置 from rest_framework.pagi ...

  5. 「Django」rest_framework学习系列-视图

    方式一 1.settings设置 INSTALLED_APPS = [ ... 'rest_framework', ] 2.views设置 from rest_framework.response i ...

  6. 「Django」rest_framework学习系列-解析器

    满足两个要求,request.Post中才有值 1.请求头要求:请求头中的Content-Type为application/x-www-form-urlencoded 2.数据格式要求 name=x& ...

  7. 「Django」rest_framework学习系列-序列化

    序列化方式一 :在业务类里序列化数据库数据 class RolesView(APIView): def get(self,request,*args,**kwargs): roles = models ...

  8. 「Django」rest_framework学习系列-版本认证

    1.自己写: class UserView(APIView): versioning_class = ParamVersion def get(self,request,*args,**kwargs) ...

  9. 「Django」rest_framework学习系列-权限认证

    权限认证:1.项目下utils文件写permissions.py文件 from rest_framework.permissions import BasePermission class SVIPP ...

随机推荐

  1. NO.3:自学python之路------集合、文件操作、函数

    引言 本来计划每周完成一篇Python的自学博客,由于上一篇到这一篇遇到了过年.开学等杂事,导致托更到现在.现在又是一个新的学期,春天也越来越近了(冷到感冒).好了,闲话就说这么多.开始本周的自学Py ...

  2. netty初认识

    Netty是什么? 本质:JBoss做的一个Jar包 目的:快速开发高性能.高可靠性的网络服务器和客户端程序 优点:提供异步的.事件驱动的网络应用程序框架和工具 通俗的说:一个好使的处理Socket的 ...

  3. Python3基础-表达式和运算符

    表达式和运算符 什么是表达式? 1+2*3就是一个表达式,这里的加号和乘号叫做运算符,1.2.3叫做操作数. 1+2*3经过计算后得到的结果是7,我们可以将计算结果存放在一个变量里,result=1+ ...

  4. Beautiful Year(拆分四位数)

    Description It seems like the year of 2013 came only yesterday. Do you know a curious fact? The year ...

  5. 【分层最短路】Joyride

    http://codeforces.com/gym/101873 C 多开一维状态记录时间,d[i][t] = 经过时间t走到节点i的最小花费 每一个状态分别向"原地等待"与&qu ...

  6. c++第三次作业

    GitHub地址 https://github.com/ronghuijun/3Elevators-scheduling 实现过程 一开始打算分成三个类来写的 因为想到电梯的功能不太一样 一个只能上1 ...

  7. 博弈---威佐夫博奕(Wythoff Game)

    这个写的不错 威佐夫博奕(Wythoff Game):有两堆各若干个物品,两个人轮流从某一堆或同 时从两堆中取同样多的物品,规定每次至少取一个,多者不限,最后取光者得胜.     这种情况下是颇为复杂 ...

  8. HDU 5159 Card

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5159 题解: 考虑没一个数的贡献,一个数一次都不出现的次数是(x-1)^b,而总的排列次数是x^b, ...

  9. 【技术向】rainmeter的设计与发现

    我们在大学期间所学的那点代码知识还远远不够,于是我就自己寻找到了一款简单易懂的软件,来丰富我的代码知识. 这款软件叫rainmeter,中文叫做雨滴,是一款可以修改桌面的软件.它可以将桌面上更改出硬盘 ...

  10. 缓存-System.Web.Caching.Cache

    实现 Web 应用程序的缓存. 每个应用程序域创建一个此类的实例,只要应用程序域将保持活动状态,保持有效. 有关此类的实例的信息,请通过Cache的属性HttpContext对象或Cache属性的Pa ...