All Courses

Python - IP Address

Neha Kumawat

2 years ago

Python IP Address
Table of Content
  • Validate the IPV4 Address
  • Validate the IPV6 Address
  • Check the Type of IP Address
  • Comparison of IP Addresses
  • IP Addresses Arithmetic
            An IP address (Internet Protocol) is a basic networking concept that provides address assignation capability in a network. The python module ipaddress is widely used to verify and categorize IP addresses into IPV4 and IPV6 types.
It can also be used to compare IP address values ​​with an IP address arithmetic for handling the IP addresses

Validate the IPV4 Address

        The ip_address function verifies the IPV4 address. If the range of values is greater than 0 to 255, then it throws an error.

print (ipaddress.ip_address(u'192.168.0.255'))
print (ipaddress.ip_address(u'192.168.0.256'))
Output of the program as follows:

192.168.0.255
ValueError: u'192.168.0.256' does not appear to be an IPv4 or IPv6 address

Validate the IPV6 Address

          The ip_address function verifies the IPV6 address. If the range of values is greater than 0 to ffff, then it throws an error.

print (ipaddress.ip_address(u'FFFF:9999:2:FDE:257:0:2FAE:112D'))

#invalid IPV6 address
print (ipaddress.ip_address(u'FFFF:10000:2:FDE:257:0:2FAE:112D'))
Output of the program as follows:

ffff:9999:2:fde:257:0:2fae:112d
ValueError: u'FFFF:10000:2:FDE:257:0:2FAE:112D' does not appear to be an IPv4 or IPv6 address
Recommended blog for you   - Python - Network Environment

Check the Type of IP Address

           We can provide the IP address of various formats and the module will be able to identify valid formats. It will also indicate the IP address category.

print type(ipaddress.ip_address(u'192.168.0.255'))

print type(ipaddress.ip_address(u'2001:db8::'))

print ipaddress.ip_address(u'192.168.0.255').reverse_pointer

print ipaddress.ip_network(u'192.168.0.0/28')
Output of the program as follows:



255.0.168.192.in-addr.arpa
192.168.0.0/28

Comparison of IP Addresses

             We can make logical comparisons of IP addresses to determine if they are equal or not. We can also compare if one IP address is larger than the other by its value.

print (ipaddress.IPv4Address(u'192.168.0.2') > ipaddress.IPv4Address(u'192.168.0.1'))
print (ipaddress.IPv4Address(u'192.168.0.2') == ipaddress.IPv4Address(u'192.168.0.1'))
print (ipaddress.IPv4Address(u'192.168.0.2') != ipaddress.IPv4Address(u'192.168.0.1'))
Output of the program as follows:

True
False
True

IP Addresses Arithmetic

         We can also use arithmetic operations to manage IP addresses. We may add or remove integer values ​​to an IP address. If after adding the value of the last octet exceeds 255 the previous octet rises to accept the value. If the additional value cannot be deducted by any previous octet when a value error arises.
print (ipaddress.IPv4Address(u'192.168.0.2')+1)

print (ipaddress.IPv4Address(u'192.168.0.253')-3)

# Increases the previous octet by value 1.
print (ipaddress.IPv4Address(u'192.168.10.253')+3)

# Throws Value error
print (ipaddress.IPv4Address(u'255.255.255.255')+1)
Output of the program as follows:


192.168.0.3
192.168.0.250
192.168.11.0
AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address
I hope you enjoyed reading this article and finally, you came to know about Python IP Address.For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…  
    
Recommended course for you :
      
Recommended blogs for you :

Submit Review