Python IPy模块常用方法
简介
IPy这个第三方包主要提供了包括网段、网络掩码、广播地址、子网数、IP类型的处理等等功能。
常用方法
查看所有方法
1 | >>> dir(IPy)[ 'INT_TYPES' , 'IP' , 'IPSet' , 'IPV6_MAP_MASK' , 'IPV6_TEST_MAP' , 'IPint' , 'IPv4ranges' , 'IPv6ranges' , 'MAX_IPV4_ADDRESS' , 'MAX_IPV6_ADDRESS' , 'STR_TYPES' , '_BitTable' , '__builtins__' , '__cached__' , '__doc__' , '__file__' , '__loader__' , '__name__' , '__package__' , '__spec__' , '__version__' , '_checkNetaddrWorksWithPrefixlen' , '_checkNetmask' , '_checkPrefix' , '_count0Bits' , '_count1Bits' , '_countFollowingZeros' , '_intToBin' , '_ipVersionToLen' , '_netmaskToPrefixlen' , '_parseAddressIPv6' , '_prefixlenToNetmask' , '_remove_subprefix' , 'bisect' , 'collections_abc' , 'intToIp' , 'parseAddress' , 'types' , 'xrange' ] |
(1)获取IP的协议版本
1 2 3 | # 查看IP的协议 print(IP( "12.12.1.1" ).version()) print(IP( "fe80::6111:d4dd:b65d:1535" ).version()) |
(2)获取IP的类型
1 2 | print(IP( "192.168.0.1" ).iptype()) print(IP( "11.1.1.2" ).iptype()) |
(3)获取指定网段的IP的清单信息
1 2 3 4 5 6 7 | ip=IP( "10.0.0.0/28" ) #ip的起始点 print(ip.net()) #ip的子网掩码 print(ip.netmask()) #ip的广播地址 print(ip.broadcast()) |
(4)IP地址的反转
1 2 | #IP地址反转 ip=IP( "12.13.14.15" ) |
(5)IP地址的进制转换
1 2 3 4 5 6 7 | ip=IP( "12.13.14.15" ) #将IP地址转换为十进制 print(ip. int ()) #将IP地址转换为二进制 print(ip.strBin()) #将IP地址转换为八进制 print(ip.strHex()) |
(6)遍历获取一个网段的所有IP地址
1 2 3 4 5 6 | ip=IP( "10.0.0.0/28" ) #计算网段内ip的个数 print(ip.len()) #遍历打印网段内的每一个IP for i in ip : print(i) |
(7)子网掩码为16与子网掩码掩码为24的局域网的比较与计算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #大局域网段 print(IP( "192.168.0.0-192.168.255.255" ,make_net=True)) #小局域网段 print(IP( "192.168.1.0-192.168.1.255" ,make_net=True)) ip=(IP( "192.168.0.0/16" )) print(ip.len())#网段IP的个数 print(ip.strNormal(0))#起始IP print(ip.strNormal(1))#网段 print(ip.strNormal(2))#子网掩码 print(ip.strNormal(3))#IP段落 ip1=(IP( "192.168.1.0/24" )) print(ip1.len()) print(ip1.strNormal(0)) print(ip1.strNormal(1)) print(ip1.strNormal(2)) print(ip1.strNormal(3)) |
(8)已知一个IP和子网掩码,计算其属于的网段
1 2 3 | print(IP( '192.168.1.0' ).make_net( '255.255.255.0' )) print(IP( '192.168.1.0/255.255.255.0' , make_net = True)) print(IP( '192.168.1.0-192.168.1.255' , make_net = True)) |
(9)判断一个IP地址是否在一个网段
1 | print( "12.11.12.133" in IP( "12.11.12.0/24" )) |
(10)判断两个网段是否存在包含关系
1 2 3 | IP( '10.0.0.0/24' ) < IP(12.0.0.0/24) #类似于整型的大小比较 '192.168.1.100' in IP( '192.168.1.0/24' ) #判断IP地址和网段是否在另一个网段内 IP( '192.168.1.0/24' ) in IP( '192.168.0.0/16' ) |
(11)判断两个网段是否重叠 返回1表示重叠了
1 2 | IP( '192.168.0.0/23' ).overlaps( '192.168.1.0/24' ) IP( '192.168.1.0/24' ).overlaps( '192.168.2.0' ) |
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #coding:utf-8 from IPy import IP ip_s=raw_input( "please input an IP or net-range: " ) 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: %s' % ips.strBin()) print( 'iptype: %s' % ips.iptype()) 运行结果: C:\Users\admin\workspace\zhangnq>python IPy_test2.py please input an IP or net-range: 192.168.1.1 reverse address: 1.1.168.192. in -addr.arpa. hexadecimal: 0xc0a80101 binary: 11000000101010000000000100000001 iptype: PRIVATE C:\Users\admin\workspace\zhangnq>python IPy_test2.py please input an IP or net-range: 8.8.8.8 reverse address: 8.8.8.8. in -addr.arpa. hexadecimal: 0x8080808 binary: 00001000000010000000100000001000 iptype: PUBLIC C:\Users\admin\workspace\zhangnq>python IPy_test2.py please input an IP or net-range: 192.168.1.0/28 net: 192.168.1.0 netmask: 255.255.255.240 broadcast: 192.168.1.15 reverse address: 0.1.168.192. in -addr.arpa. subnet: 16 hexadecimal: 0xc0a80100 binary: 11000000101010000000000100000000 iptype: PRIVATE |
ipy模块用法示例脚本
一个自动识别IP地址、子网、方向解析、IP类型等信息的脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #!/usr/bin/env python # -*- coding: utf-8 -*- def ip(): try : from IPy import IP ###加载模块 ip_s = raw_input( '请输入IP地址或者网段地址:' )###输入一个IP地址或者网段 ips = IP(ip_s) #定义元素 if len(ips) > 1: #如果len出来的数字大于1,那么就是一个网段 print( '网络地址: %s' % ips.net()) print( '子网掩码: %s' % ips.netmask()) print( '网络广播地址: %s' % ips.reverseNames() [0]) print( '网络子网数: %s' % len(ips)) else : ###否则就是一个地址 print( 'IP反向解析: %s' % ips.reverseNames() [0]) print( '十六进制地址: %s' % ips.strHex()) print( '二进制地址: %s' % ips.strBin()) print( '地址类型: %s' % ips.iptype()) print time.strftime( "%Y-%m-%d %H:%M:%S" ) #code except Exception, e: logging.info( "error:" + str(e) + "\n" + traceback.format_exc()) print traceback.format_exc() finally : pass |
运行效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [root@mylinuxer python]# 192.168.1.0/24 -bash: 192.168.1.0/24: No such file or directory [root@mylinuxer python]# python python.py 请输入IP地址或者网段地址: 192.168.1.0/24 网络地址: 192.168.1.0 子网掩码: 255.255.255.0 网络广播地址: 1.168.192. in -addr.arpa. 网络子网数: 256 [root@mylinuxer python]# python python.py 请输入IP地址或者网段地址: 192.168.1.1 IP反向解析: 1.1.168.192. in -addr.arpa. 十六进制地址: 0xc0a80101 二进制地址: 11000000101010000000000100000001 地址类型: PRIVATE [root@mylinuxer python]# python python.py 请输入IP地址或者网段地址: 116.213.249.211 IP反向解析: 211.249.213.116. in -addr.arpa. 十六进制地址: 0x74d5f9d3 二进制地址: 01110100110101011111100111010011 地址类型: PUBLIC |
"一劳永逸" 的话,有是有的,而 "一劳永逸" 的事却极少
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)