-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
60 lines (46 loc) · 1.75 KB
/
server.py
File metadata and controls
60 lines (46 loc) · 1.75 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
import socket
import ssl
import sys
import threading
import signal
import time
from loguru import logger
from src.server.client import Client
# pylint: disable=unused-import
from src.shared import loguru_config
from src.shared.protocol import HOST
def signal_handler(_sig, _frame):
logger.info("Shutting down server...")
sys.exit(0)
def client_acceptor(bindsocket: socket.socket, context: ssl.SSLContext):
while True:
try:
sock, addr = bindsocket.accept()
logger.debug(f"Connection from {addr}")
client_sock = context.wrap_socket(sock, server_side=True)
client = Client(client_sock)
logger.info(f"Client connected: {addr}")
# Run client handler in a separate thread
t = threading.Thread(target=client.main_handler, daemon=True)
t.name = f"Client handler thread; port: {client_sock.getpeername()[1]}"
t.start()
except Exception as e:
logger.exception(f"Error accepting client: {e}")
def main():
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(certfile="server.crt", keyfile="server.key")
bindsocket = socket.socket()
bindsocket.bind(("0.0.0.0", HOST[1]))
bindsocket.listen(5)
# Importing the db module starts the connection to the database
# pylint: disable=unused-import
from src.server import db
logger.info("Server started")
threading.Thread(target=client_acceptor, args=(bindsocket, context), daemon=True).start()
# Main thread will just sleep, waiting for interrupt
while True:
time.sleep(9999)
if __name__ == "__main__":
main()