summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandy <andy@whiskeymedia.com>2012-10-07 23:12:13 -0700
committerandy <andy@whiskeymedia.com>2012-10-07 23:12:13 -0700
commit2266255b25caaf0f38af938902c9d2829c0fab72 (patch)
treec4a72f4fe7b4002f225e474f08dab0c2bbff0a0c
parent6eaa3cc8bf05fe8bbb9817d7ef274b7d98c87606 (diff)
downloadredis-py-2266255b25caaf0f38af938902c9d2829c0fab72.tar.gz
added CLIENT LIST/CLIENT KILL commands
-rw-r--r--redis/client.py20
-rw-r--r--tests/server_commands.py9
2 files changed, 27 insertions, 2 deletions
diff --git a/redis/client.py b/redis/client.py
index 45bc50f..c20ec65 100644
--- a/redis/client.py
+++ b/redis/client.py
@@ -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 @@ class StrictRedis(object):
"""
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')
diff --git a/tests/server_commands.py b/tests/server_commands.py
index 5ee7a06..87fbcd6 100644
--- a/tests/server_commands.py
+++ b/tests/server_commands.py
@@ -69,6 +69,11 @@ class ServerCommandsTestCase(unittest.TestCase):
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 @@ class ServerCommandsTestCase(unittest.TestCase):
# 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)