参考文档
https://docs.python.org/3/library/ipaddress.html
ipaddress模块包括用于处理IPv4和IPv6网络地址的类。可以用来生成、验证、查找IP地址。
可以通过传递字符串、整数或字节给ip_address()来构造地址。返回值是IPv4Address或IPv6Address实例,具体取决于所使用的地址类型。
一、安装ipaddress模块
pip install ipaddress
二、定义ip地址ipaddress.ip_address
可以通过传递一个字符串类型的ip来构建ip地址
print(ipaddress.ip_address('192.0.2.1')) IPv4Address('192.0.2.1') print(ipaddress.ip_address('2001:DB8::1')) IPv6Address('2001:db8::1')
也可以通过传递一个整数来构建ip地址
print(ipaddress.ip_address(3221225985)) IPv4Address('192.0.2.1') print(ipaddress.ip_address(42540766411282592856903984951653826561)) IPv6Address('2001:db8::1')
可以使用ip_address方法,会自动生成ipv4或ipv6类型的地址,也可以使用IPv4Address或IPv6Address来强制生成指定类型的地址。
print(ipaddress.ip_address(1)) IPv4Address('0.0.0.1') print(ipaddress.IPv4Address(1)) IPv4Address('0.0.0.1') print(ipaddress.IPv6Address(1)) IPv6Address('::1')
三、定义网络地址ipaddress.ip_network
print(ipaddress.ip_network('192.0.2.0/24')) IPv4Network('192.0.2.0/24') print(ipaddress.ip_network('2001:db8::0/96')) IPv6Network('2001:db8::/96')
默认网络地址中是不能写成主机地址的形式的,例如192.168.1.1/24不能被定义为一个网络地址,而应该定义成一个接口地址。
默认情况下,如果定义了一个接口地址作为网络地址的话,会报错
>>> ipaddress.ip_network('192.0.2.1/24') Traceback (most recent call last): ... ValueError: 192.0.2.1/24 has host bits set
但是如果设置参数strict=False则不会报错
>>> ipaddress.ip_network('192.0.2.1/24', strict=False) IPv4Network('192.0.2.0/24')
>>> ipaddress.ip_network(3221225984) IPv4Network('192.0.2.0/32') >>> ipaddress.ip_network(42540766411282592856903984951653826560) IPv6Network('2001:db8::/128')
四、定义接口地址ipaddress.ip_interface
接口地址就是网络地址中的一个主机地址,可以定义成如下形式
print(ipaddress.ip_interface('192.0.2.1/24')) IPv4Interface('192.0.2.1/24')
五、ip地址类型的检测和获取
1、获取ip对象的类型
addr4 = ipaddress.ip_address('192.0.2.1') addr6 = ipaddress.ip_address('2001:db8::1') print(addr4.version) 4 print(addr6.version) 6
2、获取ip接口对象所属的网络地址
host4 = ipaddress.ip_interface('192.0.2.1/24') print(host4.network) IPv4Network('192.0.2.0/24')
3、获取网络地址包含的主机ip数量
net4 = ipaddress.ip_network('192.0.2.0/24') print(net4.num_addresses) 256
4、获取网络地址中包含的所有主机地址
print(list(ip_network('192.0.2.0/29').hosts())) [IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'), IPv4Address('192.0.2.3'), IPv4Address('192.0.2.4'), IPv4Address('192.0.2.5'), IPv4Address('192.0.2.6')]
5、获取网络地址的子网掩码和反掩码
print(ipaddress.ip_network('1.1.1.0/24').netmask) 255.255.255.0 print(ipaddress.ip_network('1.1.1.0/24').hostmask) 0.0.0.255 print(ipaddress.ip_network('1.1.1.0/24').with_netmask) 1.1.1.0/255.255.255.0 print(ipaddress.ip_network('1.1.1.0/24').with_hostmask) 1.1.1.0/0.0.0.255
6、通过下标获取网络地址对象中的ip地址
net4 = ipaddress.ip_network('192.0.2.0/24') print(net4[1]) IPv4Address('192.0.2.1') print(net4[-1]) IPv4Address('192.0.2.255')
7、判断ip地址是否包含在网络地址中
addr4 = ipaddress.ip_address('192.0.2.1') print(addr4 in ipaddress.ip_network('192.0.2.0/24')) True
8、将ip地址对象转换成字符串或整数格式
ip_str=str(ipaddress.IPv4Address('192.168.0.1')) print(ip_str) '192.168.0.1' ip_int=int(ipaddress.IPv4Address('192.168.0.1')) print(ip_int) 3232235521
六、ip地址的算术运算
print(IPv4Address('127.0.0.2') > IPv4Address('127.0.0.1')) True print(IPv4Address('127.0.0.2') == IPv4Address('127.0.0.1')) False print(IPv4Address('127.0.0.2') + 3) IPv4Address('127.0.0.5') print(IPv4Address('127.0.0.2') - 3) IPv4Address('126.255.255.255') print(IPv4Address('255.255.255.255') + 1) Traceback (most recent call last): File "<stdin>", line 1, in <module> ipaddress.AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address
七、ip地址范围子网汇总ipaddress.summarize_address_range(first, last)
ip_summ = [ipaddr for ipaddr in ipaddress.summarize_address_range(ipaddress.IPv4Address('192.0.2.0'),ipaddress.IPv4Address('192.0.2.130'))] print(ip_summ) [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), IPv4Network('192.0.2.130/32')] 或: ip_start = ipaddress.IPv4Address('192.0.2.0') ip_end = ipaddress.IPv4Address('192.0.2.130') ip_summary_list = ipaddress.summarize_address_range(ip_start, ip_end) print(ip_summary_list) # <generator object summarize_address_range at 0x000001FDA29E9A98> print(list(ip_summary_list)) # [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), IPv4Network('192.0.2.130/32')] for ip_summary in ip_summary_list: print(ip_summary) # 192.0.2.0/25 # 192.0.2.128/31 # 192.0.2.130/32
八、ip地址网段合并
ipaddress.collapse_addresses(addresses) >>> [ipaddr for ipaddr in ipaddress.collapse_addresses([ipaddress.IPv4Network('192.0.2.0/25'), ipaddress.IPv4Network('192.0.2.128/25')])] [IPv4Network('192.0.2.0/24')]
例子:
import ipaddress ip_range = '1.1.1.1-1.1.1.10' ip_first, ip_last = ip_range.split('-') ip_first = ipaddress.ip_address(ip_first) ip_last = ipaddress.ip_address(ip_last) # 将ip地址范围按掩码进行子网汇总 ip_range_list = [str(ip) for ip in ipaddress.summarize_address_range(ip_first, ip_last)] print(ip_range_list) ['1.1.1.1/32', '1.1.1.2/31', '1.1.1.4/30', '1.1.1.8/31', '1.1.1.10/32']
九、自动检测ip对象的合法性
例如ip地址中每一个8位二进制数的范围是0-255
print(ipaddress.ip_address("192.168.0.256")) Traceback (most recent call last): ... ValueError: '192.168.0.256' does not appear to be an IPv4 or IPv6 address 可以通过try判断ip地址的合法性,再执行其他操作 try: network = ipaddress.IPv4Network(address) except ValueError: print('address/netmask is invalid for IPv4:', address)
十、其他ipaddress的方法
is_multicast 判断是否是组播地址
is_private 判断是否是私网地址
is_global 判断是否是公网地址
is_unspecified
is_reserved 是否是保留地址
is_loopback 判断是否是环回地址
is_link_local