「Django」rest_framework学习系列-节流控制
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学习系列-节流控制的更多相关文章
- 「Django」rest_framework学习系列-API访问跨域问题
#以中间件方式解决API数据访问跨域问题1.API下新建文件夹下写PY文件a.引入内置类继承: from django.middleware.common import MiddlewareMixin ...
- 「Django」rest_framework学习系列-路由
自动生成4个url路由:from rest_framework import routersrouter = routers.DefaultRouter()router.register(r'wrx' ...
- 「Django」rest_framework学习系列-渲染器
渲染器:作用于页面,JSONRenderer只是JSON格式,BrowsableAPIRenderer有页面,.AdminRenderer页面以admin形式呈现(需要在请求地址后缀添加?fromat ...
- 「Django」rest_framework学习系列-分页
分页a.分页,看第N页,每页显示N条数据方式一:使用PageNumberPagination创建分页对象,配合settings全局配置 views设置 from rest_framework.pagi ...
- 「Django」rest_framework学习系列-视图
方式一 1.settings设置 INSTALLED_APPS = [ ... 'rest_framework', ] 2.views设置 from rest_framework.response i ...
- 「Django」rest_framework学习系列-解析器
满足两个要求,request.Post中才有值 1.请求头要求:请求头中的Content-Type为application/x-www-form-urlencoded 2.数据格式要求 name=x& ...
- 「Django」rest_framework学习系列-序列化
序列化方式一 :在业务类里序列化数据库数据 class RolesView(APIView): def get(self,request,*args,**kwargs): roles = models ...
- 「Django」rest_framework学习系列-版本认证
1.自己写: class UserView(APIView): versioning_class = ParamVersion def get(self,request,*args,**kwargs) ...
- 「Django」rest_framework学习系列-权限认证
权限认证:1.项目下utils文件写permissions.py文件 from rest_framework.permissions import BasePermission class SVIPP ...
随机推荐
- kafka可靠性
文章转载自: http://blog.csdn.net/u013256816/article/details/71091774
- java 乐观锁 vs 悲观锁
在数据库的锁机制中介绍过,数据库管理系统(DBMS)中的并发控制的任务是确保在多个事务同时存取数据库中同一数据时不破坏事务的隔离性和统一性以及数据库的统一性. 悲观锁其实就是 完全同步 比如 sync ...
- BZOJ 1901 Zju2112 Dynamic Rankings 树状数组套线段树
题意概述:带修改求区间第k大. 分析: 我们知道不带修改的时候直接上主席树就可以了对吧?两个版本号里面的节点一起走在线段树上二分,复杂度是O((N+M)logN). 然而这里可以修改,主席树显然是凉了 ...
- MySQL case when 使用
case when 自定义排序时的使用 根据 case when 新的 sort字段排序 case when t2.status = 4 and t2.expire_time>UNIX_TIME ...
- "Hello World!"团队第十次会议
Scrum会议 今天是我们"Hello World!"团队第十次召开会议,博客内容是: 1.会议时间 2.会议成员 3.会议地点 4.会议内容 5.todo list 6.会议照片 ...
- c# printDialog不显示问题
1.遇到问题:同样的代码,一个可以运行成功,另一个失败.百思不得其解情况下,监视下看每一个参数的属性是否一样,但参数太多,需要时间. 主要问题一般归结为:两个项目的属性编译设置不同,果然,一个x86正 ...
- 福大软工1816 ·软工之404NoteFound团队选题报告
目录 NABCD分析引用 N(Need,需求): A(Approach,做法): B(Benefit,好处): C(Competitors,竞争): D(Delivery,交付): 初期 中期 个人贡 ...
- pcap的安装与配置
1.打开网址:www.tcpdump.org/ 下载 libpcap-1.0.0.tar.gz (512.0KB) 软件包,通过命令 tar zxvf libpcap-1.0.0.tar.gz 解压文 ...
- Winform程序在XP系统上双击运行无反应解决方法
右键程序,打开属性栏,在兼容性选项里以兼容模式运行该程序即可解决.
- Vue 定时执行函数
var app = new Vue({ el: '#app', data: { count: , }, filters: { }, mounted: function () { this.$nextT ...