Question

In: Computer Science

Using PyCharm Edu on Windows, need to ask for an IP address (input), use "ipaddress.ip_address" to...

Using PyCharm Edu on Windows, need to ask for an IP address (input), use "ipaddress.ip_address" to validate it, only if error (value or type) set the default IP to 192.168.1.1, if no error then use the original input. This is the code so far and I need help please.

def req_Address():
    while True:

        try:
            address = str(ipaddress.ip_address(input("Please enter the ip address you wish scan [192.168.1.1]: ")))
        except (ValueError, TypeError):
            print("Not a valid IP address")
        else:
            print("Setting the default address to 192.168.1.1")
            address = '192.168.1.1'
            print("We will use: " + address)
            print()

Solutions

Expert Solution

Code:

import ipaddress # importing ipaddress to check the ip address

def req_Address():  # fucntion to get the IP address
    try:
        address = input("Please enter the ip address you wish to scan[192.168.1.1]: ") # user input for ip address
        ipaddress.ip_address(address) # checking the ip address if any error occured
        print("We will use: " + address)    # printig the message
    except ValueError or TypeError: # raising exception for Value and type errors
        print("Not a valid IP address") # message for Invalid IP input
        print("Setting the default address to 192.168.1.1") # message
        address = '192.168.1.1' # setting the default IP
        print("We will use: " + address)    # printig the message
        print() # for new line
    return address # return IP address


req_Address() # calling fucntion 

Sample output:

Code: Screenshot:

Note: As the name of the function suggest that some variable is requesting the ip address so I have returned the IP address in the req_address() fucntion.

line number 17 is just for demonstration purpose.

Note: Please let me know if you have any doubt or you need any modification in the code.


Related Solutions

In Kali Linux Write a script that ask the user to enter an IP address and...
In Kali Linux Write a script that ask the user to enter an IP address and a port number, then use the provided entries from the user to perform a query on your local network and determine if the given port is open on the provide network. Need to submit solutions for both below. 1.A short report showing the functionality of your code 2. your bash script
using xsd, Create a number guessing game You would need an input box and buttons Ask...
using xsd, Create a number guessing game You would need an input box and buttons Ask the user for a number Produce a random number from 1-50 Do the comparison ​a. Make sure to provide hints back to the user if the number enter is low or high ​​-this element should be automatically added ​b. Clear the value enter in the text ​c. Give them 3 tries Whether the user wins or loses, display all the guesses along with the...
using xml, Create a number guessing game You would need an input box and buttons Ask...
using xml, Create a number guessing game You would need an input box and buttons Ask the user for a number Produce a random number from 1-50 Do the comparison             a. Make sure to provide hints back to the user if the number enter is low or high                         -this element should be automatically added             b. Clear the value enter in the text             c. Give them 3 tries Whether the user wins or loses, display all the...
i. Use command ifconfig to find out the IP address of Metasplotiable and record it as...
i. Use command ifconfig to find out the IP address of Metasplotiable and record it as your target_IP. ii. Open a terminal on Kali and try to login to Metasploitable using the command: ssh root@[target_IP]. Can you log in to the System without providing the password? iii. Use command nmap to scan all TCP ports on our target machine. iv. Exploitation: a. Open a terminal on Kali and create a new key pair using ssh-keygen. b. Check if you can...
In Java, I need a program that will ask a user to input how many tests...
In Java, I need a program that will ask a user to input how many tests they want the average of. For example, It needs to ask the user how many tests would you like the average of? When the user enters the amount of tests they want the average score of, the program needs to give them that many test scores to input. Then with the average, determine what their grade is, if 90-100=A if 80-90 = B etc....
Hello! Um, I don't understand the how to use the IP address and the subnet mask...
Hello! Um, I don't understand the how to use the IP address and the subnet mask to find Via/Next hop, Metric, or the interface aspects of the question.
using C language Create a bitwise calculator that ask user for input two input, first int,...
using C language Create a bitwise calculator that ask user for input two input, first int, bitwise operation, second int (i.e 5 & 9) only those bitwise operation are allowed: & ~ ^ | << >>. If user uses wrong operators stop program and ask again. Convert the first int and second int into 8 BITS binary (00000000) and use bitwise operator given by user to either AND, OR, XOR, etc (ie 1001 & 1111)
Consider the following IPv4 forwarding table, using CIDR. As in exercise 1, IP address bytes are...
Consider the following IPv4 forwarding table, using CIDR. As in exercise 1, IP address bytes are in hexadecimal, and “:” is used as the separator as a reminder. destination next_hop 00:0:0:0/2 A 40:0:0:0/2 B 80:0:0:0/2 C c0:0:0:0/2 D (a). To what next_hop would each of the following be routed? 63:b1:82:15, 9e:00:15:01, de:ad:be:ef (b). Explain why every IP address is routed somewhere, even though there is no default entry. Hint: convert the first bytes to binary
Need to replace the 0's in Bid/Ask column with "Bid" and the 1's with "Ask" using...
Need to replace the 0's in Bid/Ask column with "Bid" and the 1's with "Ask" using Python. For line in lines: line = line.replace(",0,","Bid") etc did not work Time,Bid\Ask,Price,Volume 05:08.627012,0,1.16198,10000000.0 05:08.627012,1,1.16232,10000000.0 05:08.627012,0,1.16198,10000000.0 05:12.721536,0,1.16209,1000000.0 05:08.627012,1,1.16232,10000000.0 05:08.627012,0,1.16198,10000000.0 05:12.721536,0,1.16209,1000000.0 05:12.729989,1,1.16218,1000000.0 05:08.627012,1,1.16232,10000000.0 05:08.627012,0,1.16198,10000000.0 05:12.735727,0,1.16208,1000000.0 05:12.729989,1,1.16218,1000000.0 05:08.627012,1,1.16232,10000000.0
USE MATLAB Write a program in Matlab that would continuously ask the user for an input...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “Mahahanap mo na ang forever mo. Sana all!” If the user’s die is smaller, it should display, “Gising na friend,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT