-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
99 lines (73 loc) · 2.97 KB
/
server.py
File metadata and controls
99 lines (73 loc) · 2.97 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
87
88
89
90
91
92
93
94
95
96
97
98
99
import socket
import threading
from commands import CommandHandler
from parser import CommandParser
from store import KedisStore
HOST = "127.0.0.1"
PORT = 6379
# 1. Added 'store' back to the parameters for your DEBUG toggle!
def handle_client(conn, addr, handler, store):
"""
Handles an individual client connection.
"""
print(f"🔌 Client connected from {addr}")
with conn:
while True:
try:
data = conn.recv(1024)
if not data:
break # Client dropped the connection
raw_command = data.decode("utf-8").strip()
if not raw_command:
continue
tokens = CommandParser.parse(raw_command)
if tokens and tokens[0] == "ERROR":
conn.sendall(tokens[1].encode("utf-8") + b"\n")
continue
if not tokens:
continue
cmd = tokens[0].upper()
# The Remote Debug Intercept
if cmd == "DEBUG":
store.debug_mode = not getattr(store, "debug_mode", False)
conn.sendall(b"OK\n")
continue
response = handler.execute(tokens)
conn.sendall(str(response).encode("utf-8") + b"\n")
except Exception as e:
error_msg = f"(error) ERR {str(e)}\n"
conn.sendall(error_msg.encode("utf-8"))
break
print(f"🔌 Connection closed: {addr}")
def boot_server():
print("🏁 Booting Kedis TCP Server...")
print("Press Ctrl+C to gracefully shut down the engine.\n")
store = KedisStore()
handler = CommandHandler(store)
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
# --- THE FIX ---
s.listen()
s.settimeout(1.0) # Wake up every 1 second to check for Ctrl+C
# ---------------
print(f"🚀 Kedis Engine running and listening on {HOST}:{PORT}")
while True:
try:
conn, addr = s.accept()
# Spin up the daemon thread for the client
thread = threading.Thread(
target=handle_client,
args=(conn, addr, handler, store),
daemon=True,
)
thread.start()
except socket.timeout:
# Nobody connected in the last 1 second.
# Silently loop back to the top and check for Ctrl+C.
continue
except KeyboardInterrupt:
print("\n🛑 Main kill switch hit. Shutting down the Kedis Engine...")
if __name__ == "__main__":
boot_server()