-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
35 lines (29 loc) · 1.15 KB
/
Copy pathserver.py
File metadata and controls
35 lines (29 loc) · 1.15 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
from skullconstants import ACCEPT_TIMEOUT_SECONDS, HOST, MAX_USERS, PORT
import select
import socket
import time
class Server:
def __init__(self):
"""
Server class constructor
Initialize sockets, configure socket settings and
accept clients for ACCEPT_TIMEOUT_SECONDS
"""
self.client_sockets = {}
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.bind((HOST, PORT))
self.server.listen()
end_time = time.time() + ACCEPT_TIMEOUT_SECONDS
print("Accepting Connections...")
while time.time() < end_time:
read_sockets, _, _ = select.select([self.server], [], [], 0) # non-blocking
if read_sockets:
client, _ = self.server.accept()
self.client_sockets[client] = len(self.client_sockets)
if len(self.client_sockets) == MAX_USERS:
break
print(f"{len(self.client_sockets)} users connected")
def close(self):
for sock in self.client_sockets:
sock.close()