-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-cli.py
39 lines (34 loc) · 1.21 KB
/
client-cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
Test the server
"""
import sys, logging
from protocol import Client, CommandError
logging.basicConfig(level=logging.WARNING, format='%(asctime)s [%(levelname)s] %(message)s')
client = Client()
print("Start miniredis client.")
print(client.info)
print("""Available commands:[GET],[SET],[DELETE],[FLUSH],[MGET],[MSET],[LPUSH],[RPUSH],[LPOP],[RPOP],[BLPOP],[BRPOP],[LLEN]
[EXPIRE],[TTL],[PERSIST]
([MULTI],[EXEC] to be complete)
[INFO],[QUIT],[EXIT]""")
while True:
command = input(">")
segments = command.split(" ")
segments[0] = segments[0].upper()
if segments[0] == "QUIT" or segments[0] == "EXIT":
break
arguments = tuple(segments)
try:
received = client.execute(*arguments)
if isinstance(received, bytes):
filename = "_".join(segments)+".bin"
with open(filename, mode='wb') as file: # b is important -> binary
file.write(received)
print(f"{sys.getsizeof(received)} bytes saved as :{filename}")
else:
print(received)
except CommandError as e:
print("Wrong error format. Please consult the manual.")
except Exception as e:
logging.error(e)
print(f"Error when executing {command}")