-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (55 loc) · 1.82 KB
/
main.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
from time import sleep
from threading import Thread
import random
import Configuration
from Connection import Connection
class stress:
connections = []
count = 0
conf = Configuration.config()
def __init__(self,conf):
self.conf = conf
self.engine()
def listen(self,conn):
req = conn.recv()
if req in self.conf.conditionMessage:
conn.send(self.conf.conditionMessage[req])
def iteration(self):
conn = Connection(self.conf.ip, self.conf.port)
try:
conn.connect()
print("Connected")
except Exception:
print("Error to connect")
conn.send(self.conf.initMessage)
index = 0
while conn.isConnected:
Thread(target=self.listen,args=(conn,)).start()
if (self.conf.random):
index = random.randrange(0,len(self.conf.messageList))
if self.conf.random or index < len(self.conf.messageList) :
conn.send(self.conf.messageList[index])
index += 1
elif index == len(self.conf.messageList):
index = 0
else:
conn.isConnected = False
conn.close()
sleep(self.conf.messageDelay)
self.connections.append(conn)
self.count -= 1
def engine(self):
while True:
try:
if self.count < self.conf.count:
Thread(target=self.iteration).start()
self.count += 1
sleep(self.conf.delay)
except KeyboardInterrupt:
print("Exiting...")
for conn in self.connections :
conn.close()
break
if __name__ == '__main__':
conf = Configuration.loadConfig("stresst.conf")
stress(conf)