-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender.py
60 lines (52 loc) · 1.54 KB
/
sender.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
#!/usr/bin/python3
# copyright 2017 Peter Dordal
# licensed under the Apache 2.0 license
import socket
from sys import argv
import os
import time
default_host = "localhost"
portnum = 5431
blockcount = 1000
cong_algorithm = 'reno'
def talk():
global portnum, blockcount, cong_algorithm
rhost = default_host
if len(argv) > 1:
blockcount = int(argv[1])
if len(argv) > 2:
rhost = argv[2]
if len(argv) > 3:
portnum = int(argv[3])
if len(argv) > 4:
cong_algorithm = argv[4]
print("Looking up address of " + rhost + "...", end="")
try:
dest = socket.gethostbyname(rhost)
except socket.gaierror as mesg:
errno,errstr=mesg.args
print("\n ", errstr);
return;
print("got it: " + dest)
addr=(dest, portnum)
s = socket.socket()
#IPPROTO_TCP = 6 # defined in /usr/include/netinet/in.h
TCP_CONGESTION = 13 # defined in /usr/include/netinet/tcp.h
cong = bytes(cong_algorithm, 'ascii')
try:
s.setsockopt(socket.IPPROTO_TCP, TCP_CONGESTION, cong)
except OSError as mesg:
errno, errstr = mesg.args
print ('congestion mechanism {} not available: {}'.format(cong_algorithm, errstr))
return
res=s.connect_ex(addr)
if res!=0:
print("connect to port ", portnum, " failed")
return
buf = bytearray(os.urandom(1000))
starttime = time.time()
for i in range(blockcount):
s.send(buf)
s.close()
print('total time: {} seconds'.format(time.time()-starttime))
talk()