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:
Here are some examples to validate whether an IPv6 IP address.
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.
# 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"))
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
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.
# 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"))
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
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.
# 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"))
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
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.
# 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
(?:(?<=::)|(?
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!
Comments