📄 subnetparse.py
字号:
# Written by John Hoffman# see LICENSE.txt for license informationtrue = 1false = 0def to_bitfield(ip): b = [] for n in ip.split('.'): n = int(n) for i in xrange(8): b.append((n & 0x80) != 0) n <<= 1 return bclass IP_List: def __init__(self, intranet = false): self.list = [] if intranet: self.set_intranet_addresses() def includes(self, ip): if len(self.list) == 0: # if the list is empty return true b = to_bitfield(ip) for map in self.list: if b[:len(map)] == map: return true return false def read_fieldlist(self, file): # reads a list from a file in the format 'aa.bb.cc.dd/len <whatever>' try: f = open(file, 'r') lines = f.readlines() f.close() except: print '*** ERROR *** could not read IP range file' return for line in lines: line = line.strip().expandtabs() if not line or line[0] == '#': continue try: x, garbage = line.split(' ',1) except: x = line try: y, garbage = x.split('#',1) except: y = x try: ip,l = y.split('/') self.list.append(to_bitfield(ip)[:int(l)]) except: print '*** WARNING *** could not parse IP range: '+y def set_intranet_addresses(self): self.list.append(to_bitfield('127.0.0.1')[:8]) self.list.append(to_bitfield('10.0.0.0')[:8]) self.list.append(to_bitfield('172.16.0.0')[:12]) self.list.append(to_bitfield('192.168.0.0')[:16]) self.list.append(to_bitfield('169.254.0.0')[:16])
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -