Python Network Programming 简明教程
Python - IP Address
IP 地址(Internet 协议)是一个基础网络概念,用于在网络中提供地址分配功能。python 模块 ipaddress 广泛用于验证和分类 IPV4 和 IPV6 类型的 IP 地址。它还可以用于比较 IP 地址值以及 IP 地址算术运算,以操作 IP 地址。
IP Address (Internet Protocol) is a fundamental networking concept that provides address assignation capability in a network. The python module ipaddress is used extensively to validate and categorize IP address to IPV4 and IPV6 type. It can also be used to do comparison of the IP address values as well as IP address arithmetic for manipulating the ip addresses.
Validate the IPV4 Address
ip_address 函数验证 IPV4 地址。如果值范围超过 0 到 255,则会引发错误。
The ip_address function validates the IPV4 address. If the range of values is beyond 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'))
当我们运行以上程序时,我们得到以下输出:
When we run the above program, we get the following output −
192.168.0.255
ValueError: u'192.168.0.256' does not appear to be an IPv4 or IPv6 address
Validate the IPV6 Address
ip_address 函数验证 IPV6 地址。如果值范围超过 0 到 ffff,则会引发错误。
The ip_address function validates the IPV6 address. If the range of values is beyond 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'))
当我们运行以上程序时,我们得到以下输出:
When we run the above program, we get the following output −
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
Check the type of IP Address
我们可以提供各种格式的 IP 地址,该模块将能够识别有效格式。它还将指明 IP 地址的哪个类别。
We can supply the IP address of various formats and the module will be able to recognize the valid formats. It will also indicate which category of IP address it is.
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')
当我们运行以上程序时,我们得到以下输出:
When we run the above program, we get the following output −
255.0.168.192.in-addr.arpa
192.168.0.0/28
Comparison of IP Addresses
我们可以对 IP 地址进行逻辑比较,找出它们是否相等。我们还可以比较一个 IP 地址是否比另一个在值上更大。
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
我们还可以应用算术运算来操作 IP 地址。我们可以向 IP 地址添加或减去整数。如果在添加后最后一个八位组的值超过 255,则前一个八位组会增加以容纳该值。如果任何前一个八位组都无法吸收额外值,则会引发值错误。
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