-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
66 lines (52 loc) · 1.89 KB
/
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
import socket
from request import Request
from response import Response
from config import Config
from datetime import datetime, timedelta
import os
import psutil
import threading
class Server:
__root_path = Config['rootPath']
def __init__(self):
self.__socket = socket.socket()
@staticmethod
def print_memory_usage():
process = psutil.Process(os.getpid())
print(f'Memory used: {process.memory_info().rss / 1024 / 1024} MB')
@staticmethod
def process_request(connection, address, data):
start_time = datetime.now().microsecond
if data is None:
print("No data received")
connection.close()
request = Request(data)
print(
f'Request received from {address[0]}. Resource: {request.url}')
response = Response(Server.__root_path, request)
connection.send(response.get_response())
connection.close()
end_time = datetime.now().microsecond
print(
f'Responded with {response.status_code} in {end_time - start_time}ms')
Server.print_memory_usage()
def start(self):
try:
port = Config['listenPort']
self.__socket.bind(('', port))
self.__socket.listen()
workers = []
while True:
print(f'Listening on port {port}...')
connection, address = self.__socket.accept()
data = connection.recv(1024)
t = threading.Thread(
target=self.process_request, args=(connection, address, data))
workers.append(t)
t.start()
for index, worker in enumerate(workers):
if not worker.is_alive():
del workers[index]
except KeyboardInterrupt:
self.__socket.close()
print('\nConnection Closed.\nGoodbye :)')