-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssl_server.py
85 lines (72 loc) · 2.42 KB
/
ssl_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import socket
import threading
import time
import ssl
HOST = 'localhost'
PORT = 5050
INACTIVITY_TIMEOUT = 30
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain('certificate.crt', 'privateKey.key')
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server_active = True
clients = []
clients_lock = threading.Lock()
def handle_client(client, address):
global server_active
while server_active:
try:
data = client.recv(1024).decode()
if not data:
break
with clients_lock:
for c in clients:
c.sendall(data.encode())
except ConnectionResetError:
break
except Exception as e:
print(f"Error handling client {address}: {e}")
break
with clients_lock:
if client in clients:
clients.remove(client)
print(f"Client {address} disconnected.")
client.close()
def start_server():
global server_active
server.listen()
print(f'Server is listening on {HOST}:{PORT}')
while server_active:
try:
server.settimeout(1)
client, address = server.accept()
server.settimeout(None)
print(f'Connection established with {address}')
ssl_client = context.wrap_socket(client, server_side=True)
with clients_lock:
clients.append(ssl_client)
thread = threading.Thread(target=handle_client, args=(ssl_client, address), daemon=True)
thread.start()
except socket.timeout:
pass
except Exception as e:
if server_active:
print(f"Error accepting connection: {e}")
break
server.close()
def monitor_activity():
global server_active
global clients
while server_active:
time.sleep(INACTIVITY_TIMEOUT)
with clients_lock:
if not clients:
print(f"No clients connected for {INACTIVITY_TIMEOUT} seconds. Shutting down server.")
server_active = False
exit()
server_thread = threading.Thread(target=start_server, daemon=True)
activity_thread = threading.Thread(target=monitor_activity, daemon=True)
server_thread.start()
activity_thread.start()
server_thread.join()
activity_thread.join()