Python IPy模块
#!/usr/bin/env python # -*- coding: utf-8 -* # Created by YangYongming at 2018/09/17 20:22 # FileName: ipy.py import IPy ip1 = IPy.IP("220.181.199.13") ip2 = IPy.IP("220.181.199.0/24") ip3 = IPy.IP("220.181.199.0/255.255.255.0") # IP 及 网段的基本处理 print(ip3.len()) # 计算IP地址个数:256 print(ip1.version()) # 输出IP地址的版本:4 print(ip2.netmask()) # 输出掩码:255.255.255.0 print(ip1.iptype()) # 输出IP地址是公网IP还是私网IP:PUBLIC print(ip1.reverseName()) # 反向解析IP地址格式:13.199.181.220.in-addr.arpa. print(ip1.reverseNames()) # 反向解析IP地址格式,返回列表格式:['13.199.181.220.in-addr.arpa.'] print(ip2.broadcast()) # 输出一个网段的广播地址:220.181.199.255 # for i in ip2: # 打印输出IP地址 # print(i) # IP地址进制转换 print(ip1.strBin()) # 转换为二进制:11011100101101010110001100001101 print(ip1.strHex()) # 转换为十六进制:0xdcb5630d print(ip1.int()) # 转换为十进制:3702874893 # 通过wantprefixlen参数“0,1,2,3,4 ”转换成自己想要的地址格式,字符串格式的 print(ip2.strNormal(0)) # 220.181.199.0 print(ip2.strNormal(1)) # 220.181.199.0/24 print(ip2.strNormal(2)) # 220.181.199.0/255.255.255.0 print(ip2.strNormal(3)) # 220.181.199.0-220.181.199.255 # 根据IP和掩码 生成 *.*.*.*/* 的格式 print(ip1.make_net('255.255.255.0')) # 获取220.181.199.13与掩码255.255.255.0计算,CDR格式:220.181.199.0/24 print(IPy.IP('220.181.199.13/255.255.255.0', make_net=True)) # 将220.181.199.13/255.255.255.0地址格式转换为220.181.199.0/24 print(IPy.IP('220.181.199.0-220.181.199.255', make_net=True)) # 将220.181.199.0-220.181.199.255地址段转换为220.181.199.0/24格式 # 判断IP地址是否在一个网段中 print("192.168.1.22" in IPy.IP("192.168.1.0/24")) #: True print("192.168.2.22" in IPy.IP("192.168.1.0/24")) #: False # 比较两个网段的大小 print(IPy.IP("192.168.3.0/24") > IPy.IP("192.168.1.0/24")) #: True # 判断两个网段是否有重叠 print(IPy.IP("192.168.3.0/24").overlaps("192.168.4.0/24")) # :0 表示不存在重叠 print(IPy.IP("192.168.2.0/23").overlaps("192.168.3.0/24")) # :1 表示存在重叠 # IP地址排序 L = ['1.4.5.32', '4.5.3.2', '1.2.3.4', '6.1.1.1'] L1 = sorted(L, key=lambda x: IPy.IP(x).int()) for i in L1: print(i)