Table of Contents
In this Python tutorial, we will cover the basics of the IPv4 and IPv6 IP addresses. In order to validate an IP address, one must know exactly what an IP address is, but more importantly, know the difference between Internet Protocol version 4 (IPv4) and the Internet Protocol version 6 (IPv6) IP addresses.
What’s an IP Address?
Each connection to the internet has a unique identifier, which is called the Internet Protocol address (IP address). The primary purpose of the IP address is allow multiple devices to communicate and are similar to a postal address which is analogous.
In a residential home, the set-up usually includes multiple devices such as computers, laptops, phones, tablets. All of these devices are have an identifier for each device connected to the internet. The devices are then connected to an internet connection through a router and have the same public IP address. However, if your tablet or cell phone is connected to the internet from the carriers, then these devices would have a different IP address.
IPv4 Address
Internet Protocol version 4 (IPv4) addresses are 32-bit numbers and are are typically displayed in dotted decimal notation. The network prefix and host number are the two primary parts of the 32-bit address. The IPv4 has four octets separated by decimals and these octet ranges from 0 to 255 and looks like: 19.117.63.126.
IPv6 Address
Internet Protocol version 6 (IPv6) replaced the IPv4 because of exhaustion and expansion of the Internet of Things (IoT). IPv6 has the ability to allow more devices to connect to the internet.
IPv6 contains contains eight groups of four hexadecimal digits . Each group is then separated with a colon and would like: 2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF. As you can see, the IPv6 allows much more devices to be connected to the internet when compared to the IPv4.
Python ipaddress Module
The Python ipaddress module provides functions to create, manipulate and operate on IPv4 and IPv6 addresses. These functions make it straightforward to handle various tasks related to IP addresses. In this tutorial we will validate an IP address with ipaddress.IPv4Address() and ipaddress.IPv6Address() functions. However, this module does include other functions such as checking whether or not a string represents a valid IP address or network definition, iterating over all hosts in a particular subnet and checking whether or not two hosts are on the same subnet.
Validate IPv4 and IPv6 Address Example
Input:
import ipaddress ipv4_address = ["192.168.0.5","113.234.52.124","377.1.2.5"] ipv6_address = ["2001:0db8:0a0b:12f0:0000:0000:0000:0001","2001:0db8:85a3:0000:0000:8a2e:0370:7334","2001:0db8:0a0b"] def validateIPv4(): try: for i in ipv4_address: if ipaddress.IPv4Address(i): print(f"\nValid IPv4 address: {i}") else: print(f"\nInvalid IPv4 address: {i}") except ValueError as err: print('\nIP Validation Error:', err) def validateIPv6(): try: for i in ipv6_address: if ipaddress.IPv6Address(i): print(f"\nValid IPv6 address: {i}") else: print(f"\nInvalid IPv6 address: {i}") except ValueError as err: print('\nIP Validation Error:', err) if __name__ == '__main__' : validateIPv4() validateIPv6()
Output:
Valid IPv4 address: 192.168.0.5 Valid IPv4 address: 113.234.52.124 IP Validation Error: Octet 377 (> 255) not permitted in '377.1.2.5' Valid IPv6 address: 2001:0db8:0a0b:12f0:0000:0000:0000:0001 Valid IPv6 address: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 IP Validation Error: Exactly 8 parts expected without '::' in '2001:0db8:0a0b'