$Django Rest Framework-频率组件,解析器

时间:2023-03-09 09:24:43
$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,]

内置的直接使用

随机推荐

  1. Tips and Tricks for Debugging in chrome

    Tips and Tricks for Debugging in chrome Pretty print On sources panel ,clicking on the {} on the bot ...

  2. Golang入门教程(十四)结构体和类详解

    golang中并没有明确的面向对象的说法,实在要扯上的话,可以将struct比作其它语言中的class. 类声明 type Book struct { Title string Author stri ...

  3. OpenStack配置串口显示虚机界面

    OpenStack配置串口显示虚机界面 OpenStack的horizon能够显示虚拟机的界面.horizon是web界面,在我们的电脑上,姑且称之为本地,虚拟机运行在远端服务器上,称之为远端.本地显 ...

  4. SpringBoot系列: Actuator监控

    Sprng Boot 2 actuator变动加大, 网上很多资料都都已经过期. ============================配置项============================ ...

  5. 明白生产环境中的jvm参数

    明白生产环境中的jvm参数 写代码的时候,程序写完了,发到线上去运行,跑一段时间后,程序变慢了,cpu负载高了--一堆问题出来了,所以了解一下生产环境的机器上的jvm配置是有必要的.比如说: JDK版 ...

  6. mac怎么快速回到桌面 隐藏所有窗口

    当你同时按下Option+Command+h键,就能把所有已打开的程序窗口(不包括当前正在运行的应用程序窗口)最小化到Dock栏上.注意不是关闭哦,是最小化哦.如果需要把程序窗口恢复到屏幕上,直接点击 ...

  7. 五、文件IO——dup 函数

    5.1 dup 函数---复制文件描述符 5.1.1 简单cat实现及输入输出重定向 io.c #include <sys/types.h> #include <sys/stat.h ...

  8. 爬虫BS4—淘女郎

    1.修改网页头 用独自的py文件getheaders,随机返回header getheaders文件 import random headerstr = """Mozil ...

  9. Web项目笔记(一)JSONP跨域请求及其概念

    https://blog.****.net/u014607184/article/details/52027879 https://blog.****.net/saytime/article/deta ...

  10. G - Galactic Collegiate Programming Contest Kattis - gcpc (set使用)

    题目链接: G - Galactic Collegiate Programming Contest Kattis - gcpc 题目大意:当前有n个人,一共有m次提交记录,每一次的提交包括两个数,st ...