Permalink
Browse files

added CLIENT LIST/CLIENT KILL commands

  • Loading branch information...
1 parent 6eaa3cc commit 2266255b25caaf0f38af938902c9d2829c0fab72 andy committed Oct 8, 2012
Showing with 27 additions and 2 deletions.
  1. +20 −0 redis/client.py
  2. +7 −2 tests/server_commands.py
View
@@ -141,6 +141,17 @@ def float_or_none(response):
return float(response)
+def parse_client(response, **options):
+ parse = options['parse']
+ if parse == 'LIST':
+ clients = []
+ for c in response.splitlines():
+ clients.append(dict([pair.split('=') for pair in c.split(' ')]))
+ return clients
+ elif parse == 'KILL':
+ return bool(response)
+
+
def parse_config(response, **options):
if options['parse'] == 'GET':
response = [nativestr(i) if i is not None else None for i in response]
@@ -207,6 +218,7 @@ class StrictRedis(object):
),
'BGSAVE': lambda r: r == 'Background saving started',
'BRPOPLPUSH': lambda r: r and r or None,
+ 'CLIENT': parse_client,
'CONFIG': parse_config,
'DEBUG': parse_debug_object,
'HGETALL': lambda r: r and pairs_to_dict(r) or {},
@@ -372,6 +384,14 @@ def bgsave(self):
"""
return self.execute_command('BGSAVE')
+ def client_kill(self, address):
+ "Disconnects the client at ``address`` (ip:port)"
+ return self.execute_command('CLIENT', 'KILL', address, parse='KILL')
+
+ def client_list(self):
+ "Returns a list of currently connected clients"
+ return self.execute_command('CLIENT', 'LIST', parse='LIST')
+
def config_get(self, pattern="*"):
"Return a dictionary of configuration based on the ``pattern``"
return self.execute_command('CONFIG', 'GET', pattern, parse='GET')
View
@@ -69,6 +69,11 @@ def test_delitem(self):
del self.client['a']
self.assertEquals(self.client.get('a'), None)
+ def test_client_list(self):
+ clients = self.client.client_list()
+ self.assert_(isinstance(clients[0], dict))
+ self.assert_('addr' in clients[0])
+
def test_config_get(self):
data = self.client.config_get()
self.assert_('maxmemory' in data)
@@ -210,11 +215,11 @@ def test_pexpireat(self):
# expire at in unix time (milliseconds)
expire_at_seconds = int(time.mktime(expire_at.timetuple())) * 1000
self.assertEquals(self.client.pexpireat('a', expire_at_seconds), True)
- self.assertEquals(self.client.ttl('a'), 60)
+ self.assert_(self.client.ttl('a') <= 60)
# expire at given a datetime object
self.client['b'] = 'bar'
self.assertEquals(self.client.pexpireat('b', expire_at), True)
- self.assertEquals(self.client.ttl('b'), 60)
+ self.assert_(self.client.ttl('b') <= 60)
def test_get_set_bit(self):
self.assertEquals(self.client.getbit('a', 5), False)

0 comments on commit 2266255

Please sign in to comment.