目录
1.创建地址对象——ipaddress.ip_address()函数
2.定义ip网络——ipaddress.ip_network()函数
ipaddress模块作用——创建、检查和操作IP地址
1.创建地址对象——ipaddress.ip_address()函数
函数作用——根据传入值(整数(十进制/十六进制/八进制/二进制)或字符串)自动创建IPV4/IPV6地址,函数自动判断地址类别
>>>ipaddress.ip_address(1234) #十进制
IPv4Address('0.0.4.210')
>>>ipaddress.ip_address(0b101010101) #二进制,低9位
IPv4Address('0.0.1.85')
>>>ipaddress.ip_address('192.0.2.1')
IPv4Address('192.0.2.1')
>>>print(ip.version) # 打印IP版本号
>>>print(str(ip)) # 转换为字符串
>>>print(int(ip)) # 转换为十进制
>>>print(hex(int(ip))) # 转换为十六进制
>>>print(bin(int(ip))) # 转换为二进制
>>>print(oct(int(ip))) # 转换为八进制
>>>print(ip.packed) # 转换为字节
2.定义ip网络——ipaddress.ip_network()函数
(1)创建网络——使用格式为:ipaddress.ip_network('网络地址/掩码位数'),例如:
>>> a=list(ipaddress.ip_network('192.168.1.0/24'))
>>> print(a[15:25])
[IPv4Address('192.168.1.15'), IPv4Address('192.168.1.16'), IPv4Address('192.168.1.17'), IPv4Address('192.168.1.18'), IPv4Address('192.168.1.19'), IPv4Address('192.168.1.20'), IPv4Address('192.168.1.21'), IPv4Address('192.168.1.22'), IPv4Address('192.168.1.23'), IPv4Address('192.168.1.24')]
默认情况下,如果入参中设置了主机位,如‘192.168.1.1/24’,则会弹出错误提示——设置了主机位:
>>> ipaddress.ip_network('192.168.1.1/24')
Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
ipaddress.ip_network('192.168.1.1/24')
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\ipaddress.py", line 74, in ip_network
return IPv4Network(address, strict)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\ipaddress.py", line 1536, in __init__
raise ValueError('%s has host bits set' % self)
ValueError: 192.168.1.1/24 has host bits set
可以通过设置strict=False实现附加位强制为0:
>>> ipaddress.ip_network('192.168.1.1/24',strict=False)
IPv4Network('192.168.1.0/24')
常用查询和操作命令
net=ipaddress.ip_network('192.168.1.0/24')
3.查看网络中独立地址个数
>>>net.num_addresses
256
4.打印主机地址
>>> for x in net.hosts():
print(x)
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
...
192.168.1.252
192.168.1.253
192.168.1.254
5.获取网络掩码
>>> net.netmask
IPv4Address('255.255.255.0')
6.剔除部分ip段
>>> net2=ipaddress.ip_network('192.168.1.0/26')
>>> list(net.address_exclude(net2))
[IPv4Network('192.168.1.128/25'), IPv4Network('192.168.1.64/26')]
即:192.168.1.64
192.168.1.65
---
192.168.1.127
192.168.1.128
---
192.168.1.253
192.168.1.254
7.拆分网络段
>>> list(net.subnets(new_prefix=25))
[IPv4Network('192.168.1.0/25'), IPv4Network('192.168.1.128/25')]
即以25位子网掩码拆分net网络段,划分为两个网络段
8.ip接口
>>>ief = ipaddress.ip_interface('192.168.1.1/23')
>>>print(ief.ip) # 打印ip地址
192.168.1.1
>>>print(ief.network) # 打印其所属网段
192.168.0.0/23
>>>print(ief.with_netmask) # 以带子网掩码形式打印
192.168.1.1/255.255.254.0
>>>print(ief.with_prefixlen) # 已有掩码个数(前缀)形式打印
192.168.1.1/23
9.比较ip地址
>>>print(ipaddress.ip_network('192.168.1.1/32').compare_networks(ipaddress.ip_network('192.168.1.2/32')))
-1
>>>print(ipaddress.ip_network('192.168.1.1/32').compare_networks(ipaddress.ip_network('192.168.1.1/32')))
0
>>>print(ipaddress.ip_network('192.168.1.1/32').compare_networks(ipaddress.ip_network('192.168.1.0/32')))
1