$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,]
内置的直接使用
随机推荐
- sqlalchemy数据库分层操作
在学习sqlalchemy操作中,最常见的就是如下的示例,一个文件基本上包含了数据库的增删改查.sqlalchemy的具体操作我就不再详细说明了.流程大概是: 定义表 创建数据库引擎 创建表 插入数据 ...
- maven项目的聚合与继承
maven项目的聚合与继承: 一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 1 <modules> 2 <module>模 ...
- Docker-02 无人值守安装 docker
#!/bin/bash # # 无人值守安装 docker # # # .关闭SELinux # setenforce sed -i 's/SELINUX=enforcinf/SELINUX=disa ...
- C#正则表达式匹配
1.匹配所有带括号的字符串 string s = "aaaa(bbb)ccc(ddd)eeee"; string pattern = "\\(\\w+\\)"; ...
- extjs.net 按钮执行并显示Mask代码
<ext:Button ID="ButtonTest" runat="server" Width="65" Text="同 ...
- spring注解第05课 FactoryBean
1.工厂bean调用 @Configuration public class MainConfig2 {/** * 使用Spring提供的 FactoryBean(工厂Bean); * 1).默认获取 ...
- 十二、文件和目录——Linux文件系统结构
12.1 Linux文件系统结构 12.1.1 文件操作基本元素 文件操作相关的最基本元素是:目录结构,索引节点和文件的数据本身 目录结构(目录项) 索引节点(i 节点) 文件的数据 12.1.2 文 ...
- bean的装配方式(注入方式,构造注入,setter属性注入)
bean的装配方式有两种,构造注入和setter属性注入. public class User { private String username; private String password; ...
- 调用kaldi的模型进行解码
At the moment Kaldi is targeted more at people who are building ASR systems than those who just want ...
- IDEA对新建java线程池的建议
1 代码片段 ExecutorService pool = Executors.newCachedThreadPool(); 2 建议的三种模板 A 第一种,采用Apache的common.lang3 ...