Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions redis/_parsers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,22 @@ def parse_zadd(response, **options):
def parse_client_list(response, **options):
clients = []
for c in str_if_bytes(response).splitlines():
# Values might contain '='
clients.append(dict(pair.split("=", 1) for pair in c.split(" ")))
client_dict = {}
tokens = c.split(" ")
last_key = None
for token in tokens:
if "=" in token:
# Values might contain '='
key, value = token.split("=", 1)
client_dict[key] = value
last_key = key
else:
# Values may include spaces. For instance, when running Redis via a Unix socket — such as
# "/tmp/redis sock/redis.sock" — the addr or laddr field will include a space.
client_dict[last_key] += " " + token

if client_dict:
clients.append(client_dict)
return clients


Expand Down
22 changes: 21 additions & 1 deletion tests/test_parsers/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from redis._parsers.helpers import parse_info
from redis._parsers.helpers import parse_info, parse_client_list


def test_parse_info():
Expand Down Expand Up @@ -61,3 +61,23 @@ def test_parse_info_list_dict_mixed():

assert isinstance(info["list_two"], dict)
assert info["list_two"] == {"a b": "foo", "c": True, "d": "bar", "e": True}


def test_parse_client_list():
response = "id=7 addr=/tmp/redis sock/redis.sock:0 fd=9 name=test=_complex_[name] age=-1 idle=0 cmd=client|list user=default lib-name=go-redis(,go1.24.4) lib-ver="
expected = [
{
"id": "7",
"addr": "/tmp/redis sock/redis.sock:0",
"fd": "9",
"name": "test=_complex_[name]",
"age": "-1",
"idle": "0",
"cmd": "client|list",
"user": "default",
"lib-name": "go-redis(,go1.24.4)",
"lib-ver": "",
}
]
clients = parse_client_list(response)
assert clients == expected