Dev Patel (100828640) screenshot of 3 results from the 3 tests: CODE: import socket # Here I am importing the socket module from Python library def handle(req): # defining a function named handle that takes one arguments called req """Handle a request to the function Args: req (str): request body containing the port number """ udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Useing IPv4 address by adding socket.AF_INET and creating a socket for UDP communication with IP. udp.settimeout(1) socket # setting a timeout of 1 second to block operations on the tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Useing IPv4 address by adding socket.AF_INET and creating a socket for TCP communication with IP. print("Dev Patel") # Printing my name as per assignment requirement try: hostname = socket.gethostname() # The hostname will stores the hostname retrieved from gethostname function via socket module udp.connect(('10.254.254.254', 1)) address '10.254.254.254' on port 1. ip = udp.getsockname()[0] assigned to the socket # it will establish a connection from socket to the IP # The ip will stores the retrieved local IP address which was try: port = int(req) to port # it will convert the variable req into an integer and assigns if 0 <= port <= 65535: between 0 to 65535 # setting a range for allowed ports which is anywhere udp_result = udp.connect_ex((ip, port)) # it will try to connect UDP connection via ip and provided port number useing connect_ex of UDP socket tcp_result = tcp.connect_ex(('localhost', port)) # it will try to connect TCP connection to the localhost ip via provided port number useing connect_ex of TCP socket if udp_result == 0: successful # If udp_result is 0, it means that the UDP connection was response = "Port {}: UDP OPEN".format(port) # And it will print port number with UDP OPEN text elif tcp_result == 0: successful, # If tcp_result is 0, it means that the TCP connection was response = "Port {}: TCP OPEN".format(port) # And it will print port number with TCP OPEN text else: # if anything other than 0 then response = "Port {}: UDP CLOSED and TCP CLOSED".format(port) # it will print port is closed else: # if prot is not in range than response = "Port not in range" except ValueError: response = "Sorry, invalid port number" # it will print port is not in range # if input is not valid integer than # it will print invalid port number except: hostname = 'Unknown' found. # it print will unknown in hostname variable if no host response = "Unable to get host name" ip = '127.0.0.1' # print statement if no host found. # it will ensures that ip has a default value of 127.0.0.1. finally: udp.close() tcp.close() print("(Hostname):", hostname) print("ip:", ip) # closing the socket udp variable # closing the socket tcp variable # Printing the hostname as per assignment requirement # Printing IP as per assignment requirement print(response) # It prints the response message like error message return response # it will returns the req