-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDP_Pinger_Client.py
66 lines (49 loc) · 1.54 KB
/
UDP_Pinger_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
65
66
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import random
from socket import *
import datetime
# Initialize constants
TIMEOUT = 1.0
UDP_IP = ''
UDP_PORT = 12000
NUM_PINGS = 10
BUFF_SIZE = 1024
# Initialize variables
rtt_list = [None] * NUM_PINGS
# Create a UDP socket
clientSocket = socket(AF_INET, SOCK_DGRAM)
# Set a timeout for the socket in seconds
clientSocket.settimeout(TIMEOUT)
for ping_num in range(1, NUM_PINGS + 1):
oldNow = datetime.datetime.now()
# Create a ping message
pingMessage = 'Ping ' + str(ping_num) + ' ' + str(oldNow.time())
# Send a message
clientSocket.sendto(pingMessage.encode(), (UDP_IP, UDP_PORT))
# Attempt to receive a message
try:
message, address = clientSocket.recvfrom(BUFF_SIZE)
# Calculate the round trip time (RTT) in seconds
newNow = datetime.datetime.now()
rtt = (newNow - oldNow).total_seconds()
# Print the response message and RTT
print(message.decode())
print(rtt)
# Save off the RTT
rtt_list[ping_num - 1] = rtt
except:
print('Request timed out')
print()
# Calculate and print RTT stats
num_dropped_packets = rtt_list.count(None)
rtt_list = [f for f in rtt_list if f]
min_rtt = min(rtt_list)
max_rtt = max(rtt_list)
avg_rtt = sum(rtt_list) / len(rtt_list)
packet_loss = (num_dropped_packets / NUM_PINGS) * 100
print('RTT Statistics')
print('min RTT:\t' + str(min_rtt))
print('max RTT:\t' + str(max_rtt))
print('average RTT:\t' + str(avg_rtt))
print('packet loss:\t' + str(packet_loss) + '%')