Python - IP-Adresse

IP-Adresse (Internet Protocol) ist ein grundlegendes Netzwerkkonzept, das Adresszuweisungsfunktionen in einem Netzwerk bietet. Das Python-Modulipaddresswird häufig verwendet, um die IP-Adresse zu validieren und nach IPV4- und IPV6-Typ zu kategorisieren. Es kann auch verwendet werden, um die IP-Adresswerte sowie die IP-Adressarithmetik zum Bearbeiten der IP-Adressen zu vergleichen.

Überprüfen Sie die IPV4-Adresse

Die Funktion ip_address überprüft die IPV4-Adresse. Wenn der Wertebereich über 0 bis 255 liegt, wird ein Fehler ausgegeben.

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

Wenn wir das obige Programm ausführen, erhalten wir die folgende Ausgabe:

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

Überprüfen Sie die IPV6-Adresse

Die Funktion ip_address überprüft die IPV6-Adresse. Wenn der Wertebereich zwischen 0 und ffff liegt, wird ein Fehler ausgegeben.

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'))

Wenn wir das obige Programm ausführen, erhalten wir die folgende Ausgabe:

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

Überprüfen Sie den Typ der IP-Adresse

Wir können die IP-Adresse verschiedener Formate angeben und das Modul kann die gültigen Formate erkennen. Es wird auch angezeigt, um welche Kategorie von IP-Adressen es sich handelt.

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')

Wenn wir das obige Programm ausführen, erhalten wir die folgende Ausgabe:

 
       
         255.0.168.192.in-addr.arpa 192.168.0.0/28 
       
      

Comparison of IP Addresses

We can make a logical comparison of the IP addresses finding out if they are equal or not. We can also compare if one IP address is greater than the other in 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'))

When we run the above program, we get the following output −

True
False
True

IP Addresses Arithmetic

We can also apply arithmetic operations to manipulate IP addresses. We can add or subtract integers to an IP address. If after addition the value of the last octet goes beyond 255 then the previous octet gets incremented to accommodate the value. If the extra value can not be absorbed by any of the previous octet then a value error is raised.

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)

When we run the above program, we get the following output −

192.168.0.3
192.168.0.250
192.168.11.0
AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address