Python: Valid an IP address
IP Address Validator
Write a Python program to validate an IP address.
From Wikipedia,
An Internet Protocol address (IP address) is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. An IP address serves two main functions: host or network interface identification and location addressing.
Internet Protocol version 4 (IPv4) defines an IP address as a 32-bit number. However, because of the growth of the Internet and the depletion of available IPv4 addresses, a new version of IP (IPv6), using 128 bits for the IP address, was standardized in 1998. IPv6 deployment has been ongoing since the mid-2000s.
IP addresses are written and displayed in human-readable notations, such as 172.16.254.1 in IPv4, and 2001:db8:0:1234:0:567:8:1 in IPv6. The size of the routing prefix of the address is designated in CIDR notation by suffixing the address with the number of significant bits, e.g., 192.168.1.15/24, which is equivalent to the historically used subnet mask 255.255.255.0.
Sample Solution-1:
Python Code:
# Import the 'socket' module to work with networking functionalities.
import socket
# Define the 'addr' variable with an IP address string. This IP address is '127.0.0.2561',
# which is intentionally an invalid IP address.
addr = '127.0.0.2561'
# Start a try-except block to catch potential errors.
try:
# Use the 'socket.inet_aton()' function to attempt to convert the IP address string into a packed binary format.
socket.inet_aton(addr)
# If the 'inet_aton()' function succeeds without raising an error, it is a valid IP address.
# Print a message indicating that the IP address is valid.
print("Valid IP")
except socket.error:
# If the 'inet_aton()' function raises a 'socket.error', it is not a valid IP address.
# Print a message indicating that the IP address is invalid.
print("Invalid IP")
Sample Output:
Invalid IP
Flowchart:
Sample Solution-2:
Python Code:
# Import the 're' module to work with regular expressions.
import re
# Define a regular expression pattern 'ip_regex' to match valid IP addresses.
# This pattern is structured to match IPv4 addresses in the format 'X.X.X.X',
# where X is a number ranging from 0 to 255.
ip_regex = "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$"
# Define a function 'check_ip_address' that takes a user-provided IP address as input.
def check_ip_address(user_ip):
# Use the 're.search()' function to check if the user-provided IP address matches the 'ip_regex' pattern.
if re.search(ip_regex, user_ip):
# If the IP address matches the pattern, return "Valid IP address."
return "Valid IP address"
else:
# If the IP address does not match the pattern, return "Invalid IP address."
return "Invalid IP address"
# Test the 'check_ip_address' function with different IP addresses.
user_ip = "10.0.0.0"
print("\n", user_ip, "->", check_ip_address(user_ip))
user_ip = "10.255.255.255"
print("\n", user_ip, "->", check_ip_address(user_ip))
user_ip = "192.168.255.0"
print("\n", user_ip, "->", check_ip_address(user_ip))
user_ip = "266.1.0.2"
print("\n", user_ip, "->", check_ip_address(user_ip))
user_ip = "01.102.103.104"
print("\n", user_ip, "->", check_ip_address(user_ip))
10.0.0.0 -> Valid Ip address 10.255.255.255 -> Valid Ip address 192.168.255.0 -> Valid Ip address 266.1.0.2 -> Invalid Ip address 01.102.103.104 -> Invalid Ip address
Flowchart:
Python Code Editor:
Previous: Write a Python program to convert true to 1 and false to 0.
Next: Write a Python program to convert an integer to binary keep leading zeros.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics