-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.py
40 lines (33 loc) · 1.14 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
import threading
from socket import *
from Communication.JsonMapper import JsonMapper
ENCODING = "utf-8"
class Server:
def __init__(self, database, args):
self.database = database
self.args = args
def handle_client(self, conn, address):
print(f"New connection from {address}")
jsondata = ""
while True:
received = conn.recv(2048)
if len(received) > 0:
jsondata += received.decode(ENCODING)
else:
break
conn.close()
print(jsondata)
packet = JsonMapper.get_packet(jsondata)
print(f"packet json: {packet}")
self.database.store_alert(packet, self.args)
print("data stored")
def start(self, port):
server = socket(AF_INET, SOCK_STREAM)
host_ip = "192.168.99.106"
server.bind((host_ip, port))
server.listen()
print(f"Listening on {host_ip}:{port}")
while True:
clientconn, clientaddr = server.accept()
clientthread = threading.Thread(target=self.handle_client, args=(clientconn, clientaddr))
clientthread.start()