-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
57 lines (47 loc) · 1.62 KB
/
admin.py
File metadata and controls
57 lines (47 loc) · 1.62 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
import pickle
import re
import socket
import threading
import traceback
from lib import Tree, Client, Packet
def handle(client_class):
while True:
try:
message = client_class.socket.recv(4096)
try:
m_packet = pickle.loads(message)
except EOFError:
pass
if re.match(r'^(.+) REQUESTS FOR CONNECTING TO NETWORK ON PORT (.+)$', m_packet.Data) is not None:
match_obj = re.match(r'^(.+) REQUESTS FOR CONNECTING TO NETWORK ON PORT (.+)$', m_packet.Data)
client_class.id = match_obj.group(1)
client_class.port = int(match_obj.group(2))
print(client_class.id)
m_packet = Packet(
0, port, client_class.id, -1, f'CONNECT TO {client_class.parent.id} WITH PORT {client_class.parent.port}')
client_class.socket.sendall(pickle.dumps(m_packet))
except:
traceback.print_exc()
print(f"client {client_class.id} disconnected")
client_class.socket.close()
server.close()
break
host = '127.0.0.1'
port = 23005
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()
print("Listening...")
myTree = Tree()
while True:
try:
client_socket, address = server.accept()
c = Client(client_socket, address, 0, 0)
parent = myTree.insert(c)
c.parent = parent
thread = threading.Thread(target=handle, args=(c,))
thread.start()
except:
traceback.print_exc()
server.close()
break