![]() |
VOOZH | about |
IP (Internet Protocol) -Address is the basic fundamental concept of computer networks which provides the address assigning capabilities to a network. Python provides ipaddress module which is used to validate and categorize the IP address according to their types(IPv4 or IPv6). This module is also used for performing wide range of operation like arithmetic, comparison, etc to manipulate the IP addresses.
For validating IP addresses python uses ip_address() function provided by the ipaddress module which raises error if values of IP exceeds the range of the type of the IP address.
Output :
175.198.42.211
ValueError: '175.198.42.270' does not appear to be an IPv4 or IPv6 address
Output :
2001:db8:85a3:2bfe:70d:8a2e:370:7334
ValueError: '2001:0db8:85a3:0ff0:00000:8a2e:0370:7334' does not appear to be an IPv4 or IPv6 address
Various operations like arithmetic, comparison, type, etc can be performed on the IP addresses with the help of ipaddress module. Some operations are listed below:
Output :
<class 'ipaddress.IPv4Address'>
<class 'ipaddress.IPv4Address'>
<class 'ipaddress.IPv6Address'>
<class 'ipaddress.IPv6Address'>
Output :
True
False
True
True
Output :
129.116.255.250
175.199.43.10
AddressValueError: -1 (< 0) is not permitted as an IPv4 address
AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address
2001:db8:85a3:2bfe:70d:8a2e:370:71ea
2001:db8:85a3:2bfe:70d:8a2e:370:771c
AddressValueError: -1 (< 0) is not permitted as an IPv6 address
AddressValueError: 340282366920938463463374607431768211456 (>= 2**128) is not permitted as an IPv6 address