访问频率控制

from rest_framework.throttling import SimpleRateThrottle
import time
VISIT_RECORD = {}

class MyThrottle(object):
    """
    一分钟允许访问5次
    """
    def __init__(self):
        self.history = []

    def allow_request(self, request, view):
        # 获取用户的IP地址
        ip = request.META.get("REMOTE_ADDR", "")
        # self.key = self.get_cache_key()
        # self.cache.get(self.key, [])
        if ip not in VISIT_RECORD:
            VISIT_RECORD[ip] = [time.time(),]
        else:
            history = VISIT_RECORD[ip]
            self.history = history
            history.insert(0, time.time())
            # 确保列表时间是允许范围之内
            while self.history[0] - self.history[-1] > 60:
                self.history.pop()
            # 判断列表长度
            if not len(self.history) <= 5:
                return False
        return True

    # 等待时间
    # [最近时间,      最老时间]
    def wait(self):
        return 60-(self.history[0] - self.history[-1])
class DRFThrottle(SimpleRateThrottle):
    scope = "WD"
    def get_cache_key(self, request, view):
        # 拿IP地址
        return self.get_ident(request)

 

posted @ 2019-12-28 21:59  刀锋意志1  阅读(240)  评论(0编辑  收藏  举报