forked from ben-goldberg/routing-traffic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-traffic.py
73 lines (58 loc) · 1.92 KB
/
generate-traffic.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
# Author: Ben Goldberg
import sys, socket, select
import random, time
import util
def generate_packet(dests, my_ip):
"""
input: a list of destination nodes
output: a string to send, and the IP of a random node desination to send
it to
"""
msg = str(time.time()) + " " + my_ip
dest_ip = random.choice(dests)
return msg, dest_ip
def main(argv):
"""
Expects command line input:
python generate-traffic.py config_file.txt my_ip pkts_per_min
"""
# Parse command line args
config_file = argv[1]
my_ip = argv[2]
pkts_per_min = int(argv[3])
# Time to wait between sending packets to satisfy pkts_per_min arg
time_to_wait = (1.0/pkts_per_min) * 60.0
# Parse the config file into dictionary
config_dict = util.parse_config(config_file)
# Get a list of destination nodes
dests = config_dict["dests"]
dests.remove(my_ip)
# Make UDP socket
udpPort = 44000
try:
udpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except socket.error, msg:
print "Failed to create socket with error: " + msg
sys.exit()
# Now bind UDP socket to given port
try:
udpSocket.bind(("", udpPort))
except socket.error, msg:
print "Bind failed with error: " + msg
sys.exit()
udpSocket.setblocking(0)
while True:
# Generate one packet to a random other node
msg, dest_ip = generate_packet(dests, my_ip)
# Send msg to desired destination
udpSocket.sendto(msg, (dest_ip,udpPort))
# Receive packets
ready_to_read, _ , _ = select.select([udpSocket],[],[], .5)
for recv_socket in ready_to_read:
recv_msg, recv_ip = recv_socket.recvfrom(1024)
print recv_msg
# Sleep to enforce sending correct num of pkts per min
time.sleep(time_to_wait)
udpSocket.close()
if __name__ == "__main__":
main(sys.argv)