Mercurial > public > sg101
comparison gpp/accounts/management/commands/rate_limit_clear.py @ 506:09a9402e4a71
Added the rate_limit_clear management command, to delete IP addresses from the rate limit datastore.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 03 Dec 2011 20:46:41 +0000 |
parents | |
children | 6f5fff924877 |
comparison
equal
deleted
inserted
replaced
505:a5d11471d031 | 506:09a9402e4a71 |
---|---|
1 """ | |
2 The rate_limit_clear command is used to clear IP addresses out from our rate | |
3 limit protection database. | |
4 | |
5 """ | |
6 from optparse import make_option | |
7 import re | |
8 | |
9 from django.core.management.base import BaseCommand | |
10 import redis | |
11 | |
12 IP_RE = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$') | |
13 | |
14 | |
15 class Command(BaseCommand): | |
16 help = """Remove IP addresses from the rate limit protection datastore.""" | |
17 option_list = list(BaseCommand.option_list) + [ | |
18 make_option("--purge", action="store_true", | |
19 help="Purge all IP addresses"), | |
20 ] | |
21 | |
22 def handle(self, *args, **kwargs): | |
23 try: | |
24 con = redis.Redis() | |
25 | |
26 # get all rate-limit keys | |
27 keys = con.keys('rate-limit-*') | |
28 | |
29 # if purging, delete them all... | |
30 if kwargs['purge']: | |
31 if keys: | |
32 con.delete(*keys) | |
33 return | |
34 | |
35 # otherwise delete the ones the user asked for | |
36 ips = [] | |
37 for ip in args: | |
38 if IP_RE.match(ip): | |
39 key = 'rate-limit-%s' % ip | |
40 if key in keys: | |
41 ips.append(key) | |
42 else: | |
43 self.stdout.write('%s not found\n' % ip) | |
44 else: | |
45 self.stderr.write('invalid IP address %s\n' % ip) | |
46 | |
47 if ips: | |
48 con.delete(*ips) | |
49 | |
50 except redis.RedisError, e: | |
51 self.stderr.write('%s\n' % e) |