This is a python code import sys, time from socket import * # Get the server hos
ID: 3686516 • Letter: T
Question
This is a python code
import sys, time
from socket import *
# Get the server hostname and port as command line arguments
print 'Usage is UDPPingerClient-Handout <IP address> <Port (12000)>'
argv = sys.argv
host = argv[1]
port = argv[2]
timeout = 1 # in second
# Create UDP client socket
# Note the use of SOCK_DGRAM for UDP datagram packet
#
# STUDO-1 - Create a socket object using the IPv4 protocol for datagrams support
clientsocket = socket(AF_INET,SOCK_DGRAM)
print 'Replace with socket create code'
# STUDO-2 - Set the timeout for the clientsocket using the "timeout" variable above
# Set socket timeout as 1 second
clientsocket.settimeout(1)
print 'Replace with timeout code'
#
# Command line argument is a string in the format <IP> <Port>, 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()
#
# STUDO-3 - Send the data to the host and port supplied by the user
# Send the UDP packet with the ping message using sendto
clientSocket.sendto(message,(host, port))
print 'Replace with send code'
# STUDO-4 - Recieve the response from the server (should be in uppercase)
# Receive the server response using recvfrom, which returns the message and address of the server
# Use a buffer size of 1024
message, address = clientSocket.recvfrom(1024)
print 'Replace with receive code'
# Received time
RTTa = time.time()
# Display the server response as an output
print "Reply from " + address[0] + ": " + message
# 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
# STUDO-5 - Close the create socket using "close"
# Close the client socket
clientSocket.close()
print 'Replace with close code'
I keep getting the following error
Traceback (most recent call last):
File "line 7, in <module>
host = argv[1]
IndexError: list index out of range
I want to know why this error is occurring, and if possible how to fix it.
Explanation / Answer
You have to pass command line parameters while executing the script.Please follow these steps:
Open Terminal, cd to script location
type: py scriptName.py <any valid IP> <port>
here argv[1] = ip and argv[2] = port
import sys
# Get the server hostname and port as command line arguments
print 'Usage is UDPPingerClient-Handout <IP address> <Port (12000)>'
argv = sys.argv
host = argv[1]
port = argv[2]
timeout = 1 # in second
# Create UDP client socket
# Note the use of SOCK_DGRAM for UDP datagram packet
#
# STUDO-1 - Create a socket object using the IPv4 protocol for datagrams support
clientsocket = socket(AF_INET,SOCK_DGRAM)
print 'Replace with socket create code'
# STUDO-2 - Set the timeout for the clientsocket using the "timeout" variable above
# Set socket timeout as 1 second
clientsocket.settimeout(1)
print 'Replace with timeout code'
#
# Command line argument is a string in the format <IP> <Port>, 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()
#
# STUDO-3 - Send the data to the host and port supplied by the user
# Send the UDP packet with the ping message using sendto
clientSocket.sendto(message,(host, port))
print 'Replace with send code'
# STUDO-4 - Recieve the response from the server (should be in uppercase)
# Receive the server response using recvfrom, which returns the message and address of the server
# Use a buffer size of 1024
message, address = clientSocket.recvfrom(1024)
print 'Replace with receive code'
# Received time
RTTa = time.time()
# Display the server response as an output
print "Reply from " + address[0] + ": " + message
# 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
# STUDO-5 - Close the create socket using "close"
# Close the client socket
clientSocket.close()
print 'Replace with close code'
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.