-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
95 lines (76 loc) · 2.28 KB
/
main.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
86
87
88
89
90
91
92
93
94
import socket
import logging.config
from datetime import datetime
from multiprocessing import Process
from signal import signal, SIGTERM
LISTEN_PORT = 8000
ECHO_LOG_PATH = './logs/sessions/'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '%(levelname)3.3s [%(name)s:%(funcName)s] %(message)s',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'level': 'INFO',
'formatter': 'simple',
'stream': 'ext://sys.stdout',
},
'file': {
'class': 'logging.FileHandler',
'level': 'INFO',
'filename': './logs/echo_server_log.log',
'formatter': 'simple',
},
},
'root': {
'level': 'INFO',
'handlers': ['console', 'file'],
'propagate': True,
}
}
logging.config.dictConfig(LOGGING)
logger = logging.getLogger('')
def keep_alive(sk, addr, callback):
signal(SIGTERM, callback)
log_filename = datetime.now().strftime('%Y-%m-%d_%H:%M:%S_{}').format(addr)
with open(ECHO_LOG_PATH + log_filename, 'w') as log_file:
while True:
data = sk.recv(2048)
if not data:
callback()
return
sk.send(data)
log_file.write(str(data)[2:-1])
def get_termination_callback(sk):
def callback():
sk.shutdown(1)
sk.close()
return callback
def setup():
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
connection.bind(('0.0.0.0', LISTEN_PORT))
connection.listen(0)
thread_list = []
try:
while True:
new_socket, address = connection.accept()
addr, port = address
logger.info('connection from {}:{}'.format(addr, port))
callback = get_termination_callback(new_socket)
p = Process(target=keep_alive, args=(new_socket, addr, callback))
p.start()
thread_list.append(p)
except KeyboardInterrupt:
for p in thread_list:
p.terminate()
if __name__ == "__main__":
try:
setup()
except:
logger.exception('Something went wrong')