-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_server.py
65 lines (51 loc) · 1.62 KB
/
redis_server.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
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
#!/usr/bin/env python
import socket
import threading
from redis_process import RedisProcess
def receive_data(sock):
buffer_size = 1024
data = b'' # Initialize as bytes
chunk = sock.recv(buffer_size)
data += chunk
return data
def handle_client(client_socket, redis_process):
while True:
bytes_data = receive_data(client_socket)
if not bytes_data:
break
try:
response = redis_process.set_command(bytes_data)
except ValueError as e:
print(f"Error: {e}")
else:
client_socket.send(response)
client_socket.close()
def start_server():
try:
# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to address and port
port = 6379
server_addr = ('', port)
server_socket.bind(server_addr)
# Listen for incoming connections
server_socket.listen(50000)
#print(f"Server is listening on port {port}")
redis_process = RedisProcess()
while True:
#print("Waiting for connection")
client_socket, client_addr = server_socket.accept()
client_thread = threading.Thread(target=handle_client, args=(client_socket, redis_process))
client_thread.start()
except Exception as e:
print(f"Error starting server: {e}")
close_server()
finally:
server_socket.close()
def close_server():
print("Server is closing..")
def server_start():
try:
start_server()
except KeyboardInterrupt:
close_server()