$Django Rest Framework-频率组件,解析器
1 频率组件
#自定义组件写频率认证(重点继承BaseThrottle)
from rest_framework.throttling import BaseThrottle
import time
class Thro(BaseThrottle):
dic={}
def allow_request(self, request, view):
'''
:param request:
:param view:
:return: 布尔类型
'''
ctime=time.time()
self.ip=request.META.get('REMOTE_ADDR')
if self.ip and self.ip not in self.dic:
self.dic[self.ip]=[ctime]
return True
lis=self.dic.get(self.ip)
#把最早的时间和现在时间对比 1.大于多少秒(60s)的去掉 2.留下的是多少秒内(60秒内)的时间数 = 访问次数
while lis and ctime-lis[-1]>60:
lis.pop()
#剩下的时间列表 1.在多少秒内(60s内)2.设置访问次数:列表内时间个数=限制范文次数
if len(lis)<6:
lis[:0]=[ctime]
print (self.dic)
return True
print(self.dic)
return False
#访问超过限制触发
def wait(self):
'''
:return:多少秒过后才可以访问
'''
#最早的时间
tim=self.dic.get(self.ip)[-1]
ctime=time.time()
#ctime-tim 值越来越大
return 60-(ctime-tim) #局部使用
throttle_classes=[Thro,]
#全局使用
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': ['app01.Mythrottle.Thro', ],
}
自定义的频率组件
#settings
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': ['app01.Mythrottle.Thro', ],
'DEFAULT_THROTTLE_RATES': {
#self.scope : self.rate
'keykey': '6/m' #1分钟最大访问次数为6
} } #定义一个频率组件类(重点继承SimpleRateThrottle)
class Thro(SimpleRateThrottle):
scope = 'keykey'
def get_cache_key(self, request, view):
# 返回ip地址
# ip=request.META.get('REMOTE_ADDR')
# return ip
return self.get_ident (request)
内置的频率组件(只需配置一下,再写个类返回ip/可以返回用户名_按需求)
class Books(APIView):
#apiview的方法
# def throttled(self, request, wait):
# """
# If request is throttled, determine what kind of exception to raise.
# """
#源码:实例话了一个对象,我们只需要把这个 数据属性的英文错误改成中文
#中文显示:让后再让我们自定义类的对象调父类的ini方法
# raise exceptions.Throttled (wait)
#改中文
def throttled(self, request, wait):
from rest_framework import exceptions
class sss(exceptions.Throttled):
default_detail = '访问限制'
extra_detail_singular = '{wait}秒解除限制'
extra_detail_plural = 'Expected available in {wait} seconds.'
raise sss(wait)
def get(self,request):
response={'status':100,'msg':'请求成功'}
books=models.Book.objects.all()
ser=Myserializers.Books(books,many=True)
response['data']=ser.data
return JsonResponse(response,safe=False)
限制访问改中文信息
2 解析器
作用:传过来的数据,解析成字典
使用:
局部使用:
from rest_framework.parsers import JSONParser,FormParser
在视图类中:
parser_classes = [FormParser,]
全局使用
REST_FRAMEWORK = {
'DEFAULT_PARSER_CLASSES':[
'rest_framework.parsers.JSONParser'
] } 局部使用指定的解析器:
在视图类中:
parser_classes = [FormParser,]
内置的直接使用
随机推荐
- cocos-2dx 3.17 环境搭建 与 新建工程
由于上次在ubuntu中安装了cocos2dx的时候环境被破坏了,所以本文是基于 Windows 编写的 平台: OS : windows 10 compiler : VS 2015 下载 coc ...
- EL表达式遍历集合获取下标
如题,HTML页面很多时候需要循环遍历一个集合,并且获得集合元素得下标做判断,或者把下标传递给后台作为参数 那么我们就需要用到EL表达式的varStatus 代码一:<c:forEach var ...
- Chrome Dev Tools: Code Folding in CSS and Javascript for improved code readiability
Note : Apply for google chrome canary. You can fold code blocks in CSS (and Sass) and javascript fil ...
- 百度编辑器ueditor 光标位置的坐标
项目需求: 输入某个字符时,弹出一个弹框 弹框位置跟随光标处 经查找和亲测,下面记录一下代码: // 下面计算坐标 let domUtils = UE.dom.domUtils let bk_star ...
- vue-cli3 第三版安装搭建项目
Vue CLI是一个用于快速Vue.js开发的完整系统 3.X较2.X结构变了很多,更优雅,开发体验更好 官方:https://cli.vuejs.org/guide/ 安装:https://cli. ...
- 使用 Topshelf 创建 Windows 服务
Ø 前言 C# 创建 Windows 服务的方式有很多种,Topshelf 就是其中一种方式,而且使用起来比较简单.下面使用 Visual Studio Ultimate 2013 演示一下具体的使 ...
- Spring 快速开始 Profile 和 Bean
和maven profile类似,Spring bean definition profile 有两个组件:声明和激活. [栗子:开发测试环境使用HyperSQL 生产环境使用JNDI上下文根据配置查 ...
- 【游记】THUWC2018踹线记
Day1. 早上九点多报道,然后就是试机.一开始有一些懵,没看清门外的通知,操作起来各种懵逼.不过提前适应过了在Linux下面编程,所以问题不大.调了gedit的界面,试了一下对拍,敲了一道试机题,然 ...
- ImportError: libcudnn.so.5: cannot open shared object file: No such file or directory
ubuntu16.04安装cuda8.0,tensorflow-gpu版本后,运行时报错: ImportError: libcudnn.so.5: cannot open shared object ...
- Linux之Ubuntu下安装屏幕录像软件(SimpleScreenRecorder)【摘抄】
本博文全文属于摘抄自: (见文末处参考文献)(由于担心原博文丢失,以后查找不到,故此原文摘抄,以备日后多次查阅) 在日常工作中,有时需要对屏幕进行录像,以制作讲解文档等.下面介绍在Linux上安装屏幕 ...