IPy这个强大的Python第三方包主要提供了包括网段、网络掩码、广播地址、子网数、IP类型的处理等等功能。
sudo pip3 install IPy
详细帮助可以使用help(IPy)
>>> import IPy >>> IPy.IP("127.0.0.1").version() 4 >>> IPy.IP("10.0.0.0/24").version() 4 >>> IPy.IP("::1").version() 6
>>> ips = IPy.IP("192.168.1.0/24") >>> print(len(ips)) 256 >>> for ip in ips: ... print(ip) ... 192.168.1.0 192.168.1.1 192.168.1.2 192.168.1.3 ... 192.168.1.255
>>> ips.netmask() IP(‘255.255.255.0‘) >>> ips.prefixlen() 24 >>> ips.broadcast() IP(‘192.168.1.255‘)
>>> ip.reverseName() ‘100.1.168.192.in-addr.arpa.‘ >>> ip.strBin() ‘11000000101010000000000101100100‘ >>> ip.strHex() ‘0xc0a80164‘ >>> ip.strDec() ‘3232235876‘ >>> ip.v46map() IP(‘::ffff:192.168.1.100‘)
>>> print(IPy.IP(‘127.0.0.1‘).make_net(‘255.0.0.0‘)) 127.0.0.0/8 >>> print(IPy.IP("192.168.1.0-192.168.1.255",make_net=True)) 192.168.1.0/24 >>> print(IPy.IP("192.168.1.1/255.255.255.0",make_net=True)) 192.168.1.0/24
>>> IPy.IP("192.168.10.0/24").strNormal() ‘192.168.10.0/24‘ >>> IPy.IP("192.168.10.0/24").strNormal(0) ‘192.168.10.0‘ >>> IPy.IP("192.168.10.0/24").strNormal(1) ‘192.168.10.0/24‘ >>> IPy.IP("192.168.10.0/24").strNormal(2) ‘192.168.10.0/255.255.255.0‘ >>> IPy.IP("192.168.10.0/24").strNormal(3) ‘192.168.10.0-192.168.10.255‘
>>> "192.168.100.1" in IP("192.168.100.0/24") True >>> "192.168.200.1" in IP("192.168.100.0/24") False >>>‘192.168.1.0/24‘ in IPy.IP(‘192.168.0.0/16‘) True >>>‘192.168.1.0/24‘ in IPy.IP(‘192.168.0.0-192.168.8.255‘) True
>>> IP("192.168.100.0/23").overlaps("192.168.100.0/24") 1 # 1 代表重叠 >>> IP("192.168.100.0/25").overlaps("192.168.100.0/24") 1 >>> IP("192.168.200.0/25").overlaps("192.168.100.0/24") 0
原文:https://www.cnblogs.com/dxnui119/p/14669410.html