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

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...
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.
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,...
Python #Use an input statement to ask the user for his/her age (no prompt) and assign...
Python #Use an input statement to ask the user for his/her age (no prompt) and assign the user entry to a variable #Convert the user input to an integer and assign to a variable #Write if-elif-else statements with the following conditions and actions #Important: For each Action below, use the same variable name for the rating in all the if, elif, and else statements #Condition: user input less than or equal to 9 Action: assign the letter G as a...
Using Python Question 1 Write an input function. Then use it to supply the input to...
Using Python Question 1 Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even. Question 2 Write function that asks for input from a user. If the user types 'end', the program exits, otherwise it just keeps going.
Pythons code using idle Write an input function. Then use it to supply the input to...
Pythons code using idle Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even.
CODE IN C PLEASE Ask for user to input 4 different numbers Use pointers in calculations...
CODE IN C PLEASE Ask for user to input 4 different numbers Use pointers in calculations instead of variables to calculate the sum of those 4 numbers Print the sum Use a pointer to a pointer for print operation
You need to establish a dress code. Would you ask employees to use their best judgement?...
You need to establish a dress code. Would you ask employees to use their best judgement? Create a detailed dress code highlighting what is proper and what is improper
USE CALCULATOR AND SHOW EVERYSTEP USING CALCULATOR INCLUDING THE NUMBERS USE INPUT IN THE TEST. New...
USE CALCULATOR AND SHOW EVERYSTEP USING CALCULATOR INCLUDING THE NUMBERS USE INPUT IN THE TEST. New road signs are made with the intention of improving visibility for drivers. Highway safety engineers setup a test course that included both the old and new signs. Volunteers drove the course and rated the old and new signs in terms of visibility? (2 points each) a) Write the null and alternative hypotheses in words using “improved visibility” and “not improved visibility”. b) Describe a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT