-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_server.py
More file actions
81 lines (68 loc) · 2.33 KB
/
socket_server.py
File metadata and controls
81 lines (68 loc) · 2.33 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import base64
import json
import os
import socket
import struct
import sys
import threading
class SocketServer(object):
def __init__(self, CONTAINER_IP, PORT = 54321):
# self.ips =[]
self.targets = dict()
self.CONTAINER_IP = CONTAINER_IP
self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
self.s.bind((self.CONTAINER_IP,PORT))
self.s.listen(10)
self.clients = 0
self.stop_threads = False
def send_all(self, target, data):
target.sendall(data)
def reliable_send(self, target, command):
target.sendall(command)
def reliable_recv(self, target):
data=""
cter=True
while cter:
try:
print("Recieving")
new_recv = target.recv(4048)
print(new_recv.decode())
data += new_recv.decode()
if(not new_recv or len(new_recv.decode()) < 4048 ):
cter=False
break
except ValueError as e:
print(e)
break
print("end of reliable recv")
print("Recieved: ", data)
return data
def server(self):
while True:
if self.stop_threads:
break
self.s.settimeout(1)
try:
target, ip = self.s.accept()
self.targets[ip[0]] = target
# self.ips.append(ip[0])
print(str(ip[0]) + ":" + str(ip[1]) + " has connected")
# self.clients +=1
except Exception as e:
pass
def send_command(self, ips_list, data):
to_send = json.dumps(data).encode()
to_send = struct.pack('>I', len(to_send)) + to_send
for id_,ip in enumerate(list(self.targets.keys())):
if ip in ips_list:
self.reliable_send(self.targets[ip], to_send)
def connection_accept(self):
print("[+] waiting for Nodes to connect")
t1 = threading.Thread(target = self.server)
t1.start()
while len(self.targets.keys()) == 0:
# TO DO ADD CONDITION FOR CHECKING DATA NODE CONTAINERS ARE ACTIVE OR NOT
continue
def get_ips(self):
return list(self.targets.keys())