标签:IPY
from IPy import IPprint(IP(‘10.0.0.0/8‘).version()) #查看指定的ip段的类型IPV4或者是IPV6
print(IP(‘::1‘).version())
print(IP(‘192.168.0.0/16‘).len()) #查看指定的ip段可使用的ip数
ip = IP(‘192.168.10.37‘)
print(ip.reverseNames()) #反向解析
print(ip.iptype()) #查看ip的类型 :私网ip private 公网ip public
print(IP(‘59.151.55.32‘).iptype())
print(ip.int()) #将ip转换成整数
print(ip.strHex()) #将ip转换成16进制
print(ip.strBin()) #将ip转换成二进制
print(IP(0xc0a80a25)) #将16进制转换成ip地址
#根据ip和掩码生成ip地址段
print(IP(‘192.168.10.0‘).make_net(‘255.255.255.0‘))
print(IP(‘192.168.10.0/255.255.0.0‘,make_net=True))
print(IP(‘192.168.10.0-192.168.10.255‘,make_net=True))
print(IP(‘192.168.10.0/24‘).strNormal(0))
print(IP(‘192.168.10.0/24‘).strNormal(1))
print(IP(‘192.168.10.0/24‘).strNormal(2))
print(IP(‘192.168.10.0/24‘).strNormal(3))
print(‘192.168.10.37‘ in IP(‘192.168.10.0/24‘)) #判断ip是否在ip段中
print(‘192.168.10.0/24‘ in IP(‘192.168.0.0/16‘)) #判断ip段是否在ip段中
#判断网段是否重叠,1表示重叠,0表示不重叠
print(IP(‘192.168.0.0/23‘).overlaps(‘192.168.0.0/24‘))
print(IP(‘192.168.0.0/24‘).overlaps(‘192.168.2.2‘))
#例子
from IPy import IP
ip_s = input(‘请输入ip地址或者是网段: ‘)
ips = IP(ip_s)
if len(ips) > 1:
print(‘net: %s‘ % ips.net()) #网络地址
print(‘netmask: %s‘ % ips.netmask()) #子掩码
print(‘broadcast: %s‘ % ips.broadcast())
print(‘reverse address: %s‘ % ips.reverseNames()[0])
print(‘subnet: %s‘ % len(ips)) #子网数
else:
print(‘reverse address: %s‘ % ips.reverseNames()[0])
print(‘hexadecimal: %s‘ % ips.strHex())
print(‘binary ip: %s‘ % ips.strBin())
print(‘iptype: %s‘ % ips.iptype())
标签:IPY
原文地址:http://blog.51cto.com/xiaoshanzi/2119026