Python program to check if the given string is IPv4 or IPv6 or Invalid


An IP address is the short form of Internet Protocol address, which is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. It serves as an identifier for the device, allowing it to send and receive data over the network. There are two versions of IP addresses commonly used:

IPv4 (Internet Protocol version 4):

This is the most widely used version of IP addresses. It consists of four sets of numbers separated by periods, such as "192.168.0.1". Each set can have a value between 0 and 255, representing a total of over 4 billion unique addresses.

IPv6 (Internet Protocol version 6):

This is the newer version of IP addresses, designed to address the limitations of IPv4 and provide a much larger address space. IPv6 addresses are written in hexadecimal and separated by colons, such as "2001:0db8:85a3:0000:0000:8a2e:0370:7334". IPv6 addresses are 128 bits long and can accommodate an enormous number of unique addresses

IP addresses are essential for devices to communicate with each other over the internet. They are used to identify the source and destination of data packets during network communication.

In Python, we can work with IP addresses using various modules and libraries, such as socket, ipaddress, or by using regular expressions to validate and manipulate IP addresses. These modules provide functions and classes to parse, validate, and manipulate IP addresses in your Python programs.

There are several approaches for checking for the IPv4 or IPv6 or invalid ip addresses using python. Let’s go through the each approach.

Using ipaddress module

In python we have the module ipaddress which provides the function ip_address(), used to check if the given input is valid ip address or not. This function takes the string as the input argument.

Example

In this example we are passing the string as the input argument to the ip_address() function and checks if the given input ip address is valid or not.

import ipaddress
def check_ip_address(ip):
   try:
      ipaddress.ip_address(ip)
      if ":" in ip:
         return "IPv6"
      else:
         return "IPv4"
   except ValueError:
      return "Invalid"
ip_address = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
result = check_ip_address(ip_address)
print(ip_address,"is related to",result)  

Output

2001:0db8:85a3:0000:0000:8a2e:0370:7334 is related to IPv6

Using regular expression module

The regular expression module is used to search a defined pattern in the given input string. It is imported as re. We have a function in re module called match() which takes the pattern to check for the Ip address pattern in the given string then it will return the line where it found otherwise it returns null or None as the output.

Example

In this example we are passing the patterns of ipv4 and ipv6 as r'^(\d{1,3}\.){3}\d{1,3}$' and r'^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$' respectively to the match() function of the re module. If the match found the returns the Ipv4 or Ipv6 as per the pattern otherwise returns Invalid.

import re
def check_ip_address(ip):
   ipv4_pattern = r'^(\d{1,3}\.){3}\d{1,3}$'
   ipv6_pattern = r'^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$'
   if re.match(ipv4_pattern, ip):
      return "IPv4"
   elif re.match(ipv6_pattern, ip):
      return "IPv6"
   else:
      return "Invalid"
ip_address = "192.168.0.1"
result = check_ip_address(ip_address)
print(ip_address,"is related to",result)  

Output

192.168.0.1 is related to IPv4

Updated on: 19-Oct-2023

102 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements