IPy模块。。ip范围求交集。。

IP地址、网段的基本处理

  1. IPy模块包含IP类,使用它可以方便处理绝大部分格式为IPv6的网络和地址

    比如通过version方法来区分出IPv4和IPv6

    >>> import IPy
    >>> IPy.IP('10.0.0.0/8').version()
    4
    >>> IPy.IP('::1').version() 6
  2. 通过指定的网段输出该网段的IP个数和所有的IP地址清单

    如下:

    $ more ip.py 
    #!/usr/bin/env python
    import IPy
    
    ip = IPy.IP('192.168.0.0/16')
    print ip.len()
    for x in ip: print x
  3. 反向解析名称、IP类型、IP转换等

    >>> ip = IP('192.168.1.20')
    >>> ip.reverseNames()               #反向解析地址格式
    ['20.1.168.192.in-addr.arpa.'] >>> ip.iptype() #私网类型 'PRIVATE' >>> IP('8.8.8.8').iptype() #公网类型 'PUBLIC' >>> IP('8.8.8.8').int() #转换为整型格式 134744072 >>> IP('8.8.8.8').strHex() #转换为十六进制格式 '0x8080808' >>> IP('8.8.8.8').strBin() #转换成二进制格式 '00001000000010000000100000001000' >>> print IP('0x8080808') #十六进制转换为IP格式 8.8.8.8 >>> print IP(134744072) #整型格式转换为IP格式 8.8.8.8
  4. IP方法也支持网络地址的转换,例如根据IP和掩码产生网段格式

    >>> print (IP('192.168.1.0').make_net('255.255.255.0')) 192.168.1.0/24 >>> print (IP('192.168.1.0/255.255.255.0',make_net=True)) 192.168.1.0/24 >>> print (IP('192.168.1.0-192.168.1.255',make_net=True)) 192.168.1.0/24
  5. 通过strNormal方法指定不同wantprefixlen参数值以定制不同输出类型的网段,输出类型为字符串

    >>> IP('192.168.1.0/24').strNormal(0)   #无返回
    '192.168.1.0' >>> IP('192.168.1.0/24').strNormal(1) #prefix格式 '192.168.1.0/24' >>> IP('192.168.1.0/24').strNormal(2) #decimalnetmask格式 '192.168.1.0/255.255.255.0' >>> IP('192.168.1.0/24').strNormal(3) #lastIP格式 '192.168.1.0-192.168.1.255'

多网络计算方法详解

比较两个网段是否存在包含、重叠等关系,比如同网络但不同prefixlen会认为是不相等的网段,如10.0.0.0/16不等于10.0.0.0/24,另外即使具有相同的prefixlen但处于不同的网络地址,同样也视为不相等,如10.0.0.0/16不等于192.0.0.0/16。IPy支持类似于数值型数据的比较,以帮助IP对象进行比较。

  1. 比较IP大小

    >>> IP('10.0.0.0/24') < IP('12.0.0.0/24')
    True
  2. 判断IP地址和网段是否包含于另一个网段中

    >>> '192.168.1.100' in IP('192.168.1.0/24') True >>> IP('192.168.1.0/24') in IP('192.168.0.0/16') True
  3. 判断两个网段是否存在重叠(overlaps方法)

    >>> IP('192.168.0.0/23').overlaps('192.168.1.0/24')
    1 >>> IP('192.168.1.0/24').overlaps('192.168.2.0/24')

ipy模块简单运用:

  

  1 # -*- coding: utf-8 -*-
  2 import IPy
  3 from IPy import IP
  4 import re
  5 # 获取掩码位的网络地址
  6 def IPy_IP_ByteMask(ip):
  7     try:
  8         ips = IPy.IP(ip)
  9         # 测试用
 10         if False:
 11             for ip in ips:
 12                 # print ip
 13                 if ip == ips[0]:
 14                     print 'network: ' + str(ips[0])
 15                 elif ip == ips[1]:
 16                     print 'first: ' + str(ips[1])
 17                 elif ip == ips[-1]:
 18                     print 'broad:' + str(ips[-1])
 19                 else:
 20                     pass
 21                     # print ip
 22         return ips
 23     except ValueError, e:
 24         # 利用报错机制,在报错时对最后一位数值进行减一操作,利用自我调用尝试到正确的网络位实现数据输出
 25         b = (ip.split('/')[0]).split('.')
 26         if int(b[3]):
 27             ip = b[0] + '.' + b[1] + '.' + b[2] + '.' + str(int(b[3])-1) + '/' + ip.split('/')[-1]
 28         elif int(b[2]):
 29             ip = b[0] + '.' + b[1] + '.' + str(int(b[2])-1) + '.0' + '/' + ip.split('/')[-1]
 30         elif int(b[1]):
 31             ip = b[0] + '.' + str(int(b[1]) - 1) + '.0.0' + '/' + ip.split('/')[-1]
 32         elif int(b[0]):
 33             ip = str(int(b[1]) - 1) + '.' + '.0.0.0' + '/' + ip.split('/')[-1]
 34         return IPy_IP_ByteMask(ip)
 35     except Exception, e:
 36         return None
 37 
 38 def ip_form(range):
 39     rl = [x for x in re.split(",| |\n|;", range)]
 40     ip = []
 41     for r in rl:
 42         if '/' in r:
 43             ip_mask = IPy_IP_ByteMask(r)
 44             ip_ran = IP(ip_mask)
 45             ip.append((ip_ran[0].int(), ip_ran[-1].int()))
 46         elif '-' in r:
 47             # 处理ip范围
 48             try:
 49                 ip_ran = IP(r)
 50                 ip.append((ip_ran[0].int(), ip_ran[-1].int()))
 51             except ValueError,e:
 52                 ip0 = IP(r[:r.index('-')]).int()
 53                 ip1 = IP(r[r.index('-') + 1:]).int()
 54                 ip.append((ip0,ip1))
 55         elif '*' in r and r !='*':
 56             ip_r = r.replace('*', '0')+'-'+ r.replace('*', '255')
 57             ip_ran = IP(ip_r)
 58             ip.append((ip_ran[0].int(), ip_ran[-1].int()))
 59         elif r == '*':
 60             ip_ran=IP('255.255.255.255')
 61             ip.append((0, ip_ran.int()))
 62         else:
 63             ip_ran = IP(r)
 64             ip.append(ip_ran.int())
 65     return ip
 66 
 67 def ip_range_intersection(range1, range2):
 68     ip1 = ip_form(range1)
 69     ip2 = ip_form(range2)
 70     inum = 0
 71     unnum = 0
 72     for i in ip1:
 73         for y in ip2:
 74             if type(i)== tuple and type(y)==tuple:
 75                 if i[0] > y[1] or i[1] < y[0]:
 76                     unnum+=1
 77                 else:
 78                     inum += 1
 79             if type(i) == tuple and type(y) == int:
 80                 if i[0] <= y <= i[1]:
 81                     inum += 1
 82             if type(i)== int and type(y) == tuple:
 83                 if y[0] <= i <= y[1]:
 84                     inum += 1
 85             else:
 86                 if i == y:
 87                     inum += 1
 88     if inum >= 1:
 89         return True
 90     else:
 91         return False
 92 if __name__ == '__main__':
 93 
 94     test = [
 95         [True, '10.65.10.65', '10.65.10.1-10.65.10.66'],
 96         [True, '10.65.10.65,10.65.10.231', '10.65.10.1-10.65.10,10.65.10.88-10.65.10.234'],
 97         [True, '10.65.10.1-10.65.10.5,10.65.10.10-10.65.10.52', '10.65.10.40-10.65.10.78'],
 98         [True, '10.65.10.1-10.65.10.66,10.65.10.50-10.65.10.90', '10.65.10.100,10.65.10.48/28'],
 99         [True, '10.65.10.65,10.65.10.231,10.65.10.72', '10.65.10.1,10.65.10.2,10.65.10.*'],
100         [True, '10.65.10.65,10.65.10.231,10.65.10.72', '10.65.10.1,10.65.10.2,10.65.10.230/28'],
101         [True, '10.65.10.65-10.65.10.66', '10.65.10.*'],
102         [True, '1.1.1.1,10.10.10.10', '*'],
103         [False, '10.65.10.65,10.65.10.231', '10.65.10.1-10.65.10.64,10.65.10.66-10.65.10.230,10.65.10.232-10.65.10.254'],
104         [False, '10.65.10.65,10.65.10.231', '10.65.12.*'],
105         [False, '10.65.10.65-10.65.40.231', '10.65.1.1-10.65.10.60,10.65.80.65'],
106         [False, '10.65.10.65/20', '10.65.66.1/23']
107     ]
108 
109     for t in test:
110         try:
111             print t
112             ret = ip_range_intersection(t[1], t[2])
113             assert ret == t[0], \
114                 'ERROR: ip_range_intersection(%s, %s) return %s != %s' % (t[1], t[2], ret, t[0])
115         except Exception, e:
116             print repr(e)
117             exit(0)
118 
119     print 'Success'
View Code
    

猜你喜欢

转载自www.cnblogs.com/nanyu/p/9661044.html
今日推荐