1.安装 netaddr 组件
pip install netaddr -i https://mirrors.aliyun.com/pypi/simple/
from netaddr import IPNetwork print('https://ipjisuanqi.com/#121.224.143.188') ip_str = "121.224.143.188/28" ip = IPNetwork(ip_str) print(ip) print('============> 30 <=============') # 修改地址的掩码长度为28 ip.prefixlen = 30 print(ip) print('网络地址 => %s' % ip.network) print('网掩码位 => %s' % ip.prefixlen) print('子网掩码 => %s' % ip.netmask) ipIdx = 0; for item in ip.iter_hosts(): ipIdx = ipIdx + 1 print('可用IP %s = > %s' % (ipIdx, item)) print('可用IP数量 => %s' % ipIdx) print('广播地址 => %s' % ip.broadcast) print('============> 以上实现了网站中获取的信息 <=============')
print('============> IPAddress的用法 <=============') from netaddr import IPAddress # 判断地址的公网、私网属性 print(IPAddress("121.224.143.188").is_private()) print(IPAddress("192.168.0.1").is_private())