-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathserver.py
59 lines (39 loc) · 1.55 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
import constants
from connection import Connection
import socket
import logging
import multiprocessing
class Server:
"""
Something something docstring.
"""
def __init__(self, address, port, processor):
logging.info("Initializing server. [%s %d]", address, port)
self.processor = processor
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind((address, port))
def serve(self):
logging.info("Serving... ")
self.socket.listen(0)
while True:
sock, (remote_addr, remote_port) = self.socket.accept()
logging.info("Accepting connection from: %s (%d)", remote_addr, remote_port)
process = multiprocessing.Process(target=self.handle, args=[sock])
process.daemon = True
process.start()
logging.info("Spawned process %d to handle connection.", process.pid)
def handle(self, sock):
try:
connection = Connection(sock)
# First two messages are config and parameter scripts. Read these.
config = next(connection)
parameters = next(connection)
self.processor(connection, config, parameters)
end = constants.GadgetMessageIdentifier.pack(constants.GADGET_MESSAGE_CLOSE)
sock.send(end)
except Exception as e:
logging.exception(e)
finally:
sock.shutdown(socket.SHUT_RDWR)
sock.close()