def monitor_abuse(self, ip): """ Track the number of hits per second for a given IP. If the count is over ABUSE_THRESHOLD block user """ cache_key = self.ABUSE_PREFIX + ip abuse_count = cache.get(cache_key) if self.DEBUG: print >> sys.stderr, "BANISH ABUSE COUNT: ", abuse_count print >> sys.stderr, "BANISH CACHE KEY: ", cache_key over_abuse_limit = False if not abuse_count: cache.set(cache_key, 1, 60) else: if abuse_count >= self.ABUSE_THRESHOLD: over_abuse_limit = True # Store IP Abuse in memcache and database ban = Banishment( ban_reason="IP Abuse limit exceeded", type="ip-address", condition=ip, ) ban.save() cache.set(self.BANISH_PREFIX + ip, "1") cache.incr(cache_key) return over_abuse_limit
def monitor_abuse(self, ip): """ Track the number of hits per second for a given IP. If the count is over ABUSE_THRESHOLD banish user """ cache_key = self.ABUSE_PREFIX + ip abuse_count = cache.get(cache_key) if self.DEBUG: print >> sys.stderr, "ABUSE COUNT: ", abuse_count over_abuse_limit = False if not abuse_count: cache.set(cache_key, 1, 60) else: if abuse_count >= self.ABUSE_THRESHOLD: over_abuse_limit = True # Store IP Abuse in memcache and database ban = Banishment( ban_reason="IP Abuse limit exceeded", type="ip-address", condition=ip, ) ban.save() cache.set(self.BANISH_PREFIX + ip, "1") cache.incr(cache_key) return over_abuse_limit