-
Notifications
You must be signed in to change notification settings - Fork 71
/
client.py
64 lines (51 loc) · 1.7 KB
/
client.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
import socket
import time
import argparse
DEFAULT_PORT = 65432
DEFAULT_BUFFER_SIZE = 65536
def main(host, port, buf_size, rate_mbps):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((host, port))
print(f"Connected to {host}:{port}")
# Send the rate to the server
s.sendall(rate_mbps.to_bytes(4, byteorder="big"))
counter = 0
start_time = time.time()
try:
while True:
data = s.recv(buf_size)
if not data:
break
counter += len(data)
current_time = time.time()
if current_time - start_time >= 1:
speed_mbps = (
(counter * 8) / (1000 * 1000) / (current_time - start_time)
)
print(f"Current speed: {speed_mbps:.2f} Mbps")
counter = 0
start_time = current_time
except KeyboardInterrupt:
print("\nInterrupted by user")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="TCP Brutal example client")
parser.add_argument("host", help="Server host", type=str)
parser.add_argument("rate_mbps", help="Rate in Mbps", type=int)
parser.add_argument(
"-p",
"--port",
help="Server port",
type=int,
default=DEFAULT_PORT,
)
parser.add_argument(
"-b",
"--buffer",
help="Buffer size",
type=int,
default=DEFAULT_BUFFER_SIZE,
)
args = parser.parse_args()
main(args.host, args.port, args.buffer, args.rate_mbps)