自定制频率
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | # 自定制频率类,需要写两个方法 - # 判断是否限次:没有限次可以请求True,限次了不可以请求False def allow_request( self , request, view): - # 限次后调用,显示还需等待多长时间才能再访问,返回等待的时间seconds def wait( self ): # 代码 import time class IPThrottle(): #定义成类属性,所有对象用的都是这个 VISIT_DIC = {} def __init__( self ): self .history_list = [] def allow_request( self , request, view): ''' #(1)取出访问者ip #(2)判断当前ip不在访问字典里,添加进去,并且直接返回True,表示第一次访问,在字典里,继续往下走 #(3)循环判断当前ip的列表,有值,并且当前时间减去列表的最后一个时间大于60s,把这种数据pop掉,这样列表中只有60s以内的访问时间, #(4)判断,当列表小于3,说明一分钟以内访问不足三次,把当前时间插入到列表第一个位置,返回True,顺利通过 #(5)当大于等于3,说明一分钟内访问超过三次,返回False验证失败 ''' ip = request.META.get( 'REMOTE_ADDR' ) ctime = time.time() if ip not in self .VISIT_DIC: self .VISIT_DIC[ip] = [ctime,] return True self .history_list = self .VISIT_DIC[ip] #当前访问者时间列表拿出来 while True : if ctime - self .history_list[ - 1 ]> 60 : self .history_list.pop() # 把最后一个移除 else : break if len ( self .history_list)< 3 : self .history_list.insert( 0 ,ctime) return True else : return False def wait( self ): # 当前时间,减去列表中最后一个时间 ctime = time.time() return 60 - (ctime - self .history_list[ - 1 ]) #全局使用,局部使用 ``` ```python # SimpleRateThrottle源码分析 def get_rate( self ): """ Determine the string representation of the allowed request rate. """ if not getattr ( self , 'scope' , None ): msg = ( "You must set either `.scope` or `.rate` for '%s' throttle" % self .__class__.__name__) raise ImproperlyConfigured(msg) try : return self .THROTTLE_RATES[ self .scope] # scope:'user' => '3/min' except KeyError: msg = "No default throttle rate set for '%s' scope" % self .scope raise ImproperlyConfigured(msg) def parse_rate( self , rate): """ Given the request rate string, return a two tuple of: <allowed number of requests>, <period of time in seconds> """ if rate is None : return ( None , None ) #3 mmmmm num, period = rate.split( '/' ) # rate:'3/min' num_requests = int (num) duration = { 's' : 1 , 'm' : 60 , 'h' : 3600 , 'd' : 86400 }[period[ 0 ]] return (num_requests, duration) def allow_request( self , request, view): if self .rate is None : return True #当前登录用户的ip地址 self .key = self .get_cache_key(request, view) # key:'throttle_user_1' if self .key is None : return True # 初次访问缓存为空,self.history为[],是存放时间的列表 self .history = self .cache.get( self .key, []) # 获取一下当前时间,存放到 self.now self .now = self .timer() # Drop any requests from the history which have now passed the # throttle duration # 当前访问与第一次访问时间间隔如果大于60s,第一次记录清除,不再算作一次计数 # 10 20 30 40 # self.history:[10:23,10:55] # now:10:56 while self .history and self .now - self .history[ - 1 ] > = self .duration: self .history.pop() # history的长度与限制次数3进行比较 # history 长度第一次访问0,第二次访问1,第三次访问2,第四次访问3失败 if len ( self .history) > = self .num_requests: # 直接返回False,代表频率限制了 return self .throttle_failure() # history的长度未达到限制次数3,代表可以访问 # 将当前时间插入到history列表的开头,将history列表作为数据存到缓存中,key是throttle_user_1,过期时间60s return self .throttle_success() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用