「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 ...
随机推荐
- Amazon移除差评适用范围 - Amazon request for the feedback removal
Greetings from Amazon Seller Support, Please accept my sincere apologies for the inconvenience cause ...
- socket编程 123
1. 预备知识 一直以来很少看到有多少人使用php的socket模块来做一些事情,大概大家都把它定位在脚本语言的范畴内吧,但是其实php的socket模块可以做很多事情,包括做ftplist,http ...
- Masha and Bears(翻译+思维)
Description A family consisting of father bear, mother bear and son bear owns three cars. Father bea ...
- HDU 5187 zhx's contest 快速幂,快速加
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5187 bc(中文): http://bestcoder.hdu.edu.cn/contes ...
- vim 简单用法
vim 是一个纯文本编辑器 模式化的编辑器 1:编辑模式2:输入模式3:末行模式 : 具有命令的接口,在末行模式中可以直接的通过命令修改vim编辑器打开的文本文件 模式转换 1:编辑模式—>输入 ...
- 对IT行业的一些思考
阅读完两篇报道,从“2014年十大最热门行业和职业排行榜”可以看出最热门的行业是IT行业,可以看出IT行业在未来的发展前景很乐观,选择IT行业的人也会越来越多,IT行业也会越来越庞大.但是 ...
- PHP SQL查询结果在页面上是乱码
今天系统网页出现这样一个问题:下图左边类型栏数据是没显示出来 打印SQL查询的数据是有的 原因是:————> eval函数里'return '这一字符串一定要有空格哈,没有空格,这语句就是错的. ...
- QMdiArea及QMdiSubWindow实现父子窗口及布局方法
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QMdiArea及QMdiSubWindow实现父子窗口及布局方法 本文地址:http ...
- 找xpath好用的工具(比较少用,针对只能在IE上打开的网站)
有一些网站只能在IE浏览器里打开,不像firefox那样有好多好用的插件来找元素的xpath,css path等. 当然现在IE也可以,F12出现像firebug那样的窗口,来查看元素. 这里呢在介绍 ...
- scss在ide的命令参数
%FileName% ../css/%FileBaseName%.css --sourcemap=none –style expanded