;

How to Validate Whether an IPv6 IP Address is Valid or Not in Python


Tutorialsrack 23/09/2020 Python

In this article, you will learn how to validate whether an IPv6 IP address is valid or not in python. There are many ways to validate an IPv6 IP address is valid or not.

The following list shows examples of valid IPv6 (Normal) addresses:

  • 2001 : db8: 3333 : 4444 : 5555 : 6666 : 7777 : 8888
  • 2001 : db8 : 3333 : 4444 : CCCC : DDDD : EEEE : FFFF
  • : : (implies all 8 segments are zero)
  • 2001: db8: : (implies that the last six segments are zero)
  • : : 1234 : 5678 (implies that the first six segments are zero)
  • 2001 : db8: : 1234 : 5678 (implies that the middle four segments are zero)
  • 2001:0db8:0001:0000:0000:0ab9:C0A8:0102 (This can be compressed to eliminate leading zeros, as follows: 2001:db8:1::ab9:C0A8:102 )

Here are some examples to validate whether an IPv6 IP address.

Example 1: Using ipaddress Module

In this example, we will use the ipaddress.IPv6Network() method from the ipaddress module to validate whether an IPv6 IP address is valid or not.

Here is the source code of the program to validate the IPv6 IP address.

Example 1: Using ipaddress Module
# Import Module
import ipaddress
 
# Defining a Function
def IsValid_IPv6_IPAddress(ipv6Address):
    try:
        ipaddress.IPv6Network(ipv6Address)
        return ipv6Address+" is a valid IPv6 Address"
    except ValueError as errorcode:
        #uncomment below if you want to display the exception message.
        #print(errorCode)
        #comment below if above is uncommented.
        pass
        return ipv6Address+" is not a valid IPv6 Address"
            
print(IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))
print(IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e:0370"))
print(IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e::5"))
print(IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000::2:7335"))
print(IsValid_IPv6_IPAddress("::1"))
print(IsValid_IPv6_IPAddress("::"))
print(IsValid_IPv6_IPAddress("::1234:5678"))
print(IsValid_IPv6_IPAddress("2001:db8::"))
print(IsValid_IPv6_IPAddress("0.0.0.5"))
print(IsValid_IPv6_IPAddress("5"))
print(IsValid_IPv6_IPAddress("Foo"))
print(IsValid_IPv6_IPAddress("1::5"))
Output

2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid IPv6 Address

2001:0db8:85a3:0000:0000:8a2e:0370 is not a valid IPv6 Address

2001:0db8:85a3:0000:0000:8a2e::5 is a valid IPv6 Address

2001:0db8:85a3:0000:0000::2:7335 is a valid IPv6 Address

::1 is a valid IPv6 Address

:: is a valid IPv6 Address

::1234:5678 is a valid IPv6 Address

2001:db8:: is a valid IPv6 Address

0.0.0.5 is not a valid IPv6 Address

5 is not a valid IPv6 Address

Foo is not a valid IPv6 Address

1::5 is a valid IPv6 Address

Example 2: Another Example of ipaddress Module

In this example, we used the ipaddress.ip_address() method from the ipaddress module. And this method works for both the version of IP addresses such as IPv4 and IPv6.

Here is the source code of the program to validate the IPv6 IP address is valid or not.

Example 2: Another Example of ipaddress Module
# Import Module
import ipaddress
 
# Defining a Function
def IsValid_IPv6_IPAddress(ipv6Address):
    try:
        # this method works for Both IPv4 and IPv6 IP Addresses
        ipaddress.ip_address(ipv6Address)
        return ipv6Address+" is a valid IPv6 Address"
    except ValueError as errorcode:
        #uncomment below if you want to display the exception message.
        #print(errorCode)
        #comment below if above is uncommented.
        # pass
        return ipv6Address+" is not a valid IPv6 Address"
        
print(IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))
print(IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e:0370"))
print(IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e::5"))
print(IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000::2:7335"))
print(IsValid_IPv6_IPAddress("::1"))
print(IsValid_IPv6_IPAddress("::"))
print(IsValid_IPv6_IPAddress("::1234:5678"))
print(IsValid_IPv6_IPAddress("2001:db8::"))
print(IsValid_IPv6_IPAddress("5"))
print(IsValid_IPv6_IPAddress("Foo"))
print(IsValid_IPv6_IPAddress("1::5"))
Output

2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid IPv6 Address

2001:0db8:85a3:0000:0000:8a2e:0370 is not a valid IPv6 Address

2001:0db8:85a3:0000:0000:8a2e::5 is a valid IPv6 Address

2001:0db8:85a3:0000:0000::2:7335 is a valid IPv6 Address

::1 is a valid IPv6 Address

:: is a valid IPv6 Address

::1234:5678 is a valid IPv6 Address

2001:db8:: is a valid IPv6 Address

5 is not a valid IPv6 Address

Foo is not a valid IPv6 Address

1::5 is a valid IPv6 Address

Example 3: Using socket Module

In this example, we used the socket.inet_pton() method from the socket module to validate the given IPv6 IP address is valid or Not.

Here is the source code of the program to validate the given IPv6 IP address.

Example 3: Using socket Module
# Import Module
import socket

def IsValid_IPv6_IPAddress(ipv6Address):
    try:
        # test for IPv6 
        socket.inet_pton(socket.AF_INET6, ipv6Address)
        return ipv6Address+" is a valid IPv6 IP address"
    except socket.error:
        return ipv6Address+" is not valid IPv6 IP address"


print(IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))
print(IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e:0370"))
print(IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000:8a2e::5"))
print(IsValid_IPv6_IPAddress("2001:0db8:85a3:0000:0000::2:7335"))
print(IsValid_IPv6_IPAddress("::1"))
print(IsValid_IPv6_IPAddress("::"))
print(IsValid_IPv6_IPAddress("::1234:5678"))
print(IsValid_IPv6_IPAddress("2001:db8::"))
print(IsValid_IPv6_IPAddress("0.0.0.5"))
print(IsValid_IPv6_IPAddress("5"))
print(IsValid_IPv6_IPAddress("Foo"))
print(IsValid_IPv6_IPAddress("1::5"))
Output

2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid IPv6 IP address

2001:0db8:85a3:0000:0000:8a2e:0370 is not valid IPv6 IP address

2001:0db8:85a3:0000:0000:8a2e::5 is a valid IPv6 IP address

2001:0db8:85a3:0000:0000::2:7335 is a valid IPv6 IP address

::1 is a valid IPv6 IP address

:: is a valid IPv6 IP address

::1234:5678 is a valid IPv6 IP address

2001:db8:: is a valid IPv6 IP address

0.0.0.5 is not valid IPv6 IP address

5 is not valid IPv6 IP address

Foo is not valid IPv6 IP address

1::5 is a valid IPv6 IP address

Example 4: Using Regular Expression

In this example, we used the re.compile() method from the re module of the python to validate the IPv6 IP address is valid or not.

Here is the source code of the program to validate the IPv6 IP address using a regular expression.

Example 4: Using Regular Expression
# Import Module
import re

def IsValid_IPv6_IPAddress(ip):
    """Validates IPv6 addresses.
    """
    pattern = re.compile(r"""
        ^
        \s*                         # Leading whitespace
        (?!.*::.*::)                # Only a single wildcard allowed
        (?:(?!:)|:(?=:))            # Colon iff it would be part of a wildcard
        (?:                         # Repeat 6 times:
            [0-9a-f]{0,4}           #   A group of at most four hexadecimal digits
            (?:(?<=::)|(?
Output

2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid IPv6 IP address

2001:0db8:85a3:0000:0000:8a2e:0370 is not a valid IPv6 IP address

2001:0db8:85a3:0000:0000:8a2e::5 is a valid IPv6 IP address

2001:0db8:85a3:0000:0000::2:7335 is a valid IPv6 IP address

::1 is a valid IPv6 IP address

:: is a valid IPv6 IP address

::1234:5678 is a valid IPv6 IP address

2001:db8:: is a valid IPv6 IP address

0.0.0.5 is not a valid IPv6 IP address

5 is not a valid IPv6 IP address

Foo is not a valid IPv6 IP address

1::5 is a valid IPv6 IP address

I hope this article will help you to understand how to validate whether an IPv6 IP address is valid or not in python

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags