Advertisement
Python253

is_valid_ip

Mar 3rd, 2024 (edited)
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: is_valid_ip.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. Valid IP Checker Script
  8.  
  9. This script prompts the user to enter an IP address and checks whether
  10. it is in a valid IP format (IPv4 or IPv6). It utilizes a function, valid_ip,
  11. to perform the IP validation.
  12.  
  13. Requirements:
  14. - Python 3
  15.  
  16. Usage:
  17. 1. Run the script.
  18. 2. Enter an IP address when prompted.
  19. 3. The script will display whether the entered IP address is valid or not.
  20. """
  21.  
  22. import ipaddress
  23.  
  24. def valid_ip(ip):
  25.     """Check if IP address is valid.
  26.  
  27.    Args:
  28.        ip (str): IPv4 or IPv6.
  29.  
  30.    Returns:
  31.        bool: True if in valid IP format (IPv4/IPv6), False otherwise.
  32.  
  33.    """
  34.     try:
  35.         ipaddress.ip_address(ip)
  36.         return True
  37.     except Exception:
  38.         return False
  39.  
  40. def main():
  41.     # Get user input for IP address
  42.     user_ip = input("Enter an IP address: ")
  43.  
  44.     # Check if the IP address is valid
  45.     if valid_ip(user_ip):
  46.         print("The entered IP address is Valid.")
  47.     else:
  48.         print("The entered IP address is Not Valid.")
  49.  
  50. if __name__ == "__main__":
  51.     main()
  52.  
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement