-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_client.py
More file actions
86 lines (68 loc) · 2.38 KB
/
Copy pathexample_client.py
File metadata and controls
86 lines (68 loc) · 2.38 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Пример использования Redis клиента.
"""
import sys
import os
# Добавляем src в путь
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from client import RedisClient
def main():
"""Демонстрация работы клиента."""
print("Mini Redis Client Demo")
print("=" * 40)
# Создаем клиент
client = RedisClient(host='localhost', port=6379)
try:
# Подключаемся
if not client.connect():
print("ERROR: Не удалось подключиться к серверу")
return
print("Connected to server localhost:6379")
print("\nTesting commands:")
# SET
try:
success = client.set("demo_key", "demo_value", ex=30)
print(f"SET demo_key: {'OK' if success else 'FAILED'}")
except Exception as e:
print(f"SET error: {e}")
# GET
try:
value = client.get("demo_key")
print(f"GET demo_key: {value}")
except Exception as e:
print(f"GET error: {e}")
# TTL
try:
ttl = client.ttl("demo_key")
print(f"TTL demo_key: {ttl} sec")
except Exception as e:
print(f"TTL error: {e}")
# EXISTS
try:
exists = client.exists("demo_key", "nonexistent")
print(f"EXISTS demo_key, nonexistent: {exists}")
except Exception as e:
print(f"EXISTS error: {e}")
# KEYS
try:
keys = client.keys("*")
print(f"KEYS *: {keys}")
except Exception as e:
print(f"KEYS error: {e}")
# DEL
try:
deleted = client.delete("demo_key")
print(f"DEL demo_key: {deleted} keys deleted")
except Exception as e:
print(f"DEL error: {e}")
print("\nDemo completed successfully!")
except Exception as e:
print(f"ERROR: {e}")
finally:
client.disconnect()
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] in ['-h', '--help']:
print("Использование: python example_client.py")
print("Убедитесь, что сервер запущен на localhost:6379")
sys.exit(0)
main()