zoukankan      html  css  js  c++  java
  • 访问频率:仿写源码

    #仿写

    # 流程思路 客户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])
     
    # 如果想要但接口不同 重写一个新类加进去
  • 相关阅读:
    【YbtOJ#20068】连通子图
    【YbtOJ#20067】糖果分配
    【GMOJ6801】模拟patrick
    【GMOJ6800】模拟spongebob
    【洛谷P4449】于神之怒加强版
    【洛谷P3601】签到题
    【洛谷P2408】不同子串个数
    【洛谷P3809】【模板】后缀排序
    【JZOJ1753】锻炼身体
    【GMOJ1164】求和
  • 原文地址:https://www.cnblogs.com/yanhui1995/p/9664292.html
Copyright © 2011-2022 走看看