#仿写
# 流程思路 客户ip获取 若不存在在visit_record内就添加,反之取出查看其60分钟内访问次数
,如果访问数小于三则允许。反之禁用 返回剩余解禁时间
class MyThro():
VISIT_RECORD = {}
def __init__(self):
self.history = None
def allow_request(self,request,view):
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:
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)
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()
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])
return 60 - (ctime - self.history[-1])
# 如果想要但接口不同 重写一个新类加进去