Question: In this assignment, write a python code that can send or receive a UDP packet. Fill in the relevant portions of the provided skeleton

""" In this assignment, write a python code that can send or receive a UDP packet. Fill in the relevant portions of the provided skeleton (these are the "#@" sections) file to complete the assignment and make it function as desired """

import argparse, socket, sys

MAX_BYTES = 65535

# server def server(interface, port): #@ make the appropriate socket using the socket module and assign it to the variable sock #@ invoke the bind method on the sock and pass in the pair consisting of the interface and the port #@ invoke the getsockname method on the sock and print that out so that the user can know where we are listening while True: #@ invoke the recvfrom method on the sock and pass in the maximum number of bytes that we would want to receive # note that the above returns two values, which get assigned to two variables, data is one variable and address is the other

text = data.decode('ascii') print('The client at {} says {!r}'.format(address, text)) message = 'Your data was {} bytes long'.format(len(data)) encoded_message = message.encode('ascii') #@ invoke the sendto method on the sock object, and pass in the two parameters of the encoded message and the address

# client def client(hostname, port): #@ make the appropriate socket using the socket module and assign it to the variable sock hostname = sys.argv[2] #@ invoke the connect method on the sock object and pass in the pair consisting of the hostname and port #@ print out the result of calling the getsockname method of the sock object and do so in a manner that is friendly to the user

delay = 0.1 # seconds text = input("What data should we send to the server?") # send datagram while more to send while len(text): data = text.encode('ascii') # send datagram to server while True: #@ invoke the send method on the sock object to send the data print('Waiting up to {} seconds for a reply'.format(delay)) #@ invoke the settimeout method on the sock object to set the delay to the appropriate value try: #@ invoke the recv method on the sock object to receive at most MAX_BYTES and to assign the bytes received to the variable data except socket.timeout as exc: delay *= 2 # wait even longer for the next request if delay > 2.0: raise RuntimeError('I think the server is down') #from exc else: break # we are done, and can stop looping

print('The server says {!r}'.format(data.decode('ascii'))) # get next datagram to send text = input("What data should we send to the server?") # main if __name__ == '__main__': choices = {'client': client, 'server': server} parser = argparse.ArgumentParser(description='Send and receive UDP') parser.add_argument('role', choices=choices, help='which role to take') parser.add_argument('host', help='interface the server listens at;' ' host the client sends to') parser.add_argument('-p', metavar='PORT', type=int, default=1060, help='UDP port (default 1060)') args = parser.parse_args() function = choices[args.role] function(args.host, args.p)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!