访问频率:仿写源码

#仿写

# 流程思路 客户ip获取 若不存在在visit_record内就添加,反之取出查看其60分钟内访问次数

,如果访问数小于三则允许。反之禁用 返回剩余解禁时间

class MyThro():
    VISIT_RECORD = {}
    def __init__(self):
        self.history = None
    def allow_request(self,request,view):
  #获取客户请求ip
        ip = request.META.get('REMOTE_ADDR')
        import time
        ctime = time.time()
        if ip not in self.VISIT_RECORD:
    初次登录客户
            self.VISIT_RECORD[ip] = [ctime,]
            return True
        self.history = self.VISIT_RECORD.get(ip)
        while self.history and ctime-self.history[-1]>60:
            self.history.pop()
        if len(self.history)<3:
            self.history.insert(0,ctime)
            print(self.history)
            return True
        else:
            return False
    def wait(self):
        import time
        ctime = time.time()
    返回剩余频率限制时间
        return 60 - (ctime - self.history[-1])
 
# 如果想要但接口不同 重写一个新类加进去
posted @ 2018-09-17 19:15  路口有雾  阅读(151)  评论(0编辑  收藏  举报