Monday, April 21, 2025

IP Address Validity Check - Python

import os, ipaddress

os.system('cls')

while True:
    ip = input('IP Validity Check: ')
    try:
        print(ipaddress.ip_address(ip))
        print('IP Valid')
    except:
        print('-' * 79)
        print('IP Not Valid !!!!')
    finally:
        if ip == 'q':
            print('Script Finished')
            break

  1. The first line of code imports the operating system and ipaddress modules.
  2. The os.system('cls') line clears the console screen.
  3. The while True: statement initiates an infinite loop.
  4. The ip = input('IP Validity Check: ') line prompts the user to enter an IP address to be checked for validity and stores it in the ip variable.
  5. The try: statement initiates a try-except block, which is used to handle errors in the code.
  6. The ipaddress.ip_address(ip) line checks whether the IP address entered by the user is valid or not by passing it to the ip_address() method of the ipaddress module. If it is valid, the method returns the IP address object, and the code inside the try block is executed.
  7. The print('IP Valid') line prints "IP Valid" to the console if the IP address is valid.
  8. The except: statement catches any exception that occurs in the try block.
  9. The print('-' * 79) line prints a line of 79 dashes to the console to visually separate the error message from the rest of the output.
  10. The print('IP Not Valid !!!!') line prints "IP Not Valid !!!!" to the console if the IP address entered by the user is not valid.
  11. The finally: statement is executed whether or not an exception is caught in the try block.
  12. The if ip == 'q': statement checks if the user has entered "q" to quit the program. If so, the break statement is executed, which exits the while loop and terminates the program.

Simple version (for one IP) - no error checking

import os, ipaddress

os.system('cls')

ip = input('IP Validity Check: ')

if ipaddress.ip_address(ip):
    print('OK')

The given code is used to check the validity of an IP address. Here's how it works:

  1. The code begins by importing the 'os' and 'ipaddress' modules.

  2. It then clears the console screen using the 'os.system' function with the 'cls' argument.

  3. The user is prompted to enter an IP address to check its validity using the 'input' function.

  4. The 'ipaddress.ip_address' function is used to check if the entered IP address is valid.

  5. If the IP address is valid, the program prints 'OK', indicating that the IP address is valid.

  6. If the IP address is not valid, the program raises an exception, indicating that the IP address is not valid.

In summary, the program checks the validity of the entered IP address and prints 'OK' if the IP address is valid. If the IP address is not valid, an exception is raised.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...