def POST(self): ''' 提交一个ip把这个ip添加到黑名单中,这个方法供后台调用 ''' x = web.input() print x ip = x['ip'] LOG.info('client: %s insert to blacklist, is too freq')%(ip) blacklist_create(ip)
def freq_filter(client, count_max=10, time_max=10): ''' 到频率极限返回True, 否则返回False''' client_freq = freq.get(client) time_now = time.time() if client_freq is None: freq.update({client:{'count':1, 'last_up_time':time_now}}) return '' client_freq['count'] += 1 count = client_freq.get('count') if count > count_max: last_up_time = client_freq.get('last_up_time') if time_now - last_up_time <= time_max: blacklist_create(client, timeout=60*30) LOG.info('client: %s insert to blacklist, \ is too freq, %s, %s')%(client, count_max, time_max) return True else: client_freq['last_up_time'] = time_now client_freq['count'] = 1 return False