zoukankan      html  css  js  c++  java
  • Django rest_framework 频率控制组件

    频率控制

    一、频率控制实现一

    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework.throttling import SimpleRateThrottle, BaseThrottle
    from rest_framework import exceptions
    
    # 频率控制类--> 比较好的是,可以自由控制访问频率
    class Throttle(SimpleRateThrottle):
        scope = 'least_throttle'  # 指向的是settings.py中设置的访问频率
    
        def get_cache_key(self, request, view):
            return self.get_ident(request)  # 该方法获取的是前端发来请求的用户的唯一标识,此处是ip
    

    二、自定义频率控制

    # 自定义频率类 --> 自定义的,相对较灵活,比较不好的是访问频率单一,修改起来比较麻烦
    class MyThrottle(BaseThrottle):
        VISIT_RECORD = {}
    
        def __init__(self):
            self.history = None
    
        def allow_request(self, request, view):
            # 自定义控制每分钟访问多少次,运行访问返回true,不允许访问返回false
            # (1)取出访问者ip{ip1:[第二次访问时间,第一次访问时间],ip2:[]}
            # (2)判断当前ip不在访问字典里,如果不在添加进去,并且直接返回True,表示第一次访问,在字典里,继续往下走
            # (3)循环判断当前ip的列表,有值,并且当前时间减去列表的最后一个时间大于60s,把这种数据pop掉,这样列表中只有60s以内的访问时间,
            # (4)判断,当列表小于3,说明一分钟以内访问不足三次,把当前时间插入到列表第一个位置,返回True,顺利通过
            # (5)当大于等于3,说明一分钟内访问超过三次,返回False验证失败
            # (1)取出访问者ip
            # print(request.META)
            # 取出访问者ip
            ip = request.META.get('REMOTE_ADDR')
            import time
            # 拿到当前时间
            ctime = time.time()
            # (2)判断当前ip不在访问字典里,添加进去,并且直接返回True,表示第一次访问
            if ip not in self.VISIT_RECORD:
                self.VISIT_RECORD[ip] = [ctime, ]
                return True
            # 是个当前访问者ip对应的时间列表 [第一次访问的时间,]
            self.history = self.VISIT_RECORD.get(ip)
            # (3)循环判断当前ip的列表,有值,并且当前时间减去列表的最后一个时间大于60s,把这种数据pop掉,这样列表中只有60s以内的访问时间,
            while self.history and ctime - self.history[-1] > 60:
                self.history.pop()
            # (4)判断,当列表小于3,说明一分钟以内访问不足三次,把当前时间插入到列表第一个位置,返回True,顺利通过
            # (5)当大于等于3,说明一分钟内访问超过三次,返回False验证失败
            if len(self.history) < 3:
                self.history.insert(0, ctime)
                return True
            else:
                return False
    

    三、视图函数

    # Create your views here.
    class Books(APIView):
        throttle_classes = [Throttle, ]  # 继承SimpleRateThrottle的控制类
        # throttle_classes = [MyThrottle, ]  # 自定义频率控制类
    """
        # 源代码中,当超过访问频率的时候,会返回一条detail数据,而内容信息是英文的,此处想将其改为中文,
        # 下面的代码只是改了前一部分的内容,后面一部分内容并没有修改成功
        detail = '您的访问频率太高,请等一等啊!'
        def throttled(self, request, wait):
            raise exceptions.Throttled(wait, detail=self.detail)
    """
        def get(self, request):
            return Response('ok')
    



  • 相关阅读:
    BEC listen and translation exercise 44
    中译英12
    BEC listen and translation exercise 43
    中译英11
    BEC listen and translation exercise 42
    中译英10
    BEC listen and translation exercise 41
    中译英9
    BEC listen and translation exercise 40
    中译英8
  • 原文地址:https://www.cnblogs.com/xt12321/p/11134780.html
Copyright © 2011-2022 走看看