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...
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)
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,...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT