-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtraffic2.py
65 lines (52 loc) · 1.66 KB
/
traffic2.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
import socket
import sys
import time
import random
import os
from threading import *
'''
Host waits join_time seconds before creating a socket to server
Host sends msg to the server
Host makes reqConns number of connections to server
Each connection lasts inter_time seconds
Total duration = x * [join_time + (reqConns * inter_time)]
'''
def nettraffic():
# run for a long time
x = 0
while (x < 15):
server = '10.0.0.46'
port = 12345
# this is the inter-arrival time AND the start time
join_time = [j * 0.1 for j in range(30, 77)]
time.sleep(random.choice(join_time))
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except:
print 'Failed to create a socket'
sys.exit()
print 'Socket created'
s.connect((server, port))
msg = 'GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n'
# number of GET requests per connection
y = 0
reqPerConn = [1, 3, 7, 11, 13]
reqConns = random.choice(reqPerConn)
while (y < reqConns):
s.sendall(msg)
reply = s.recv(2048)
print(reply)
# duration of the connection
inter_time = [j * 0.1 for j in range(70, 110)]
time.sleep(random.choice(inter_time))
y += 1
x += 1
s.close()
if __name__ == '__main__':
print('Running')
time.sleep(30)
# each client will have range(x) simultaneous connections to the server
for i in range(10):
t = Thread(target=nettraffic)
t.start()
time.sleep(0.5)