Skip to content

Commit

Permalink
client_list (#1517)
Browse files Browse the repository at this point in the history
  • Loading branch information
AvitalFineRedis authored Jul 22, 2021
1 parent 3c244af commit ad4779e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,15 @@ def parse_client_info(value):
"key1=value1 key2=value2 key3=value3"
"""
client_info = {}
infos = value.split(" ")
infos = str_if_bytes(value).split(" ")
for info in infos:
key, value = info.split("=")
client_info[key] = value

# Those fields are definded as int in networking.c
for int_key in {"id", "age", "idle", "db", "sub", "psub",
"multi", "qbuf", "qbuf-free", "obl",
"oll", "omem"}:
"argv-mem", "oll", "omem", "tot-mem"}:
client_info[int_key] = int(client_info[int_key])
return client_info

Expand Down Expand Up @@ -620,6 +620,7 @@ class Redis:
'CLIENT ID': int,
'CLIENT KILL': parse_client_kill,
'CLIENT LIST': parse_client_list,
'CLIENT INFO': parse_client_info,
'CLIENT SETNAME': bool_ok,
'CLIENT UNBLOCK': lambda r: r and int(r) == 1 or False,
'CLIENT PAUSE': bool_ok,
Expand Down Expand Up @@ -1243,6 +1244,13 @@ def client_kill_filter(self, _id=None, _type=None, addr=None, skipme=None):
"<value> must specify at least one filter")
return self.execute_command('CLIENT KILL', *args)

def client_info(self):
"""
Returns information and statistics about the current
client connection.
"""
return self.execute_command('CLIENT INFO')

def client_list(self, _type=None):
"""
Returns a list of currently connected clients.
Expand Down
6 changes: 6 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ def test_client_list(self, r):
assert isinstance(clients[0], dict)
assert 'addr' in clients[0]

@skip_if_server_version_lt('6.2.0')
def test_client_info(self, r):
info = r.client_info()
assert isinstance(info, dict)
assert 'addr' in info

@skip_if_server_version_lt('5.0.0')
def test_client_list_type(self, r):
with pytest.raises(exceptions.RedisError):
Expand Down

0 comments on commit ad4779e

Please sign in to comment.