-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDP_Pinger_Client.py
More file actions
51 lines (41 loc) · 1.27 KB
/
UDP_Pinger_Client.py
File metadata and controls
51 lines (41 loc) · 1.27 KB
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
import sys, time
from socket import *
# Get the server hostname and port as command line arguments
#argv = sys.argv
host = 'localhost'
port = 12000
timeout = 1 # in second
# Create UDP client socket
# Note the use of SOCK_DGRAM for UDP datagram packet
clientsocket = socket(AF_INET, SOCK_DGRAM)
# Set socket timeout as 1 second
clientsocket.settimeout(timeout)
# Command line argument is a string, change the port into integer
port = int(port)
# Sequence number of the ping message
ptime = 0
# Ping for 10 times
while ptime < 10:
ptime += 1
# Format the message to be sent
data = "Ping " + str(ptime) + " " + time.asctime()
try:
# Sent time
RTTb = time.time()
# Send the UDP packet with the ping message
clientsocket.sendto(data.encode(),(host, port))
# Receive the server response
message, address = clientsocket.recvfrom(1024)
# Received time
RTTa = time.time()
# Display the server response as an output
print("Reply from " + address[0] + ": " + message.decode())
# Round trip time is the difference between sent and received time
print("RTT: " + str(RTTa - RTTb))
except:
# Server does not response
# Assume the packet is lost
print ("Request timed out.")
continue
# Close the client socket
clientsocket.close()