#! /usr/bin/env python
# -*- coding: cp936 -*-
""" IPLocator: locate IP in the QQWry.dat.
Usage:
python IPLocator.py <ip>
Create and test with Python 2.2.3.
spadger@bmy <echo.xjtu@gmail.com> 2008-2-19
"""
import socket,string,struct,sys,os
os.chdir(sys.path[0])
class IPLocator :
def __init__( self, ipdbFile ):
self.ipdb = open( ipdbFile, "rb" )
str = self.ipdb.read( 8 )
(self.firstIndex,self.lastIndex) = struct.unpack('II',str)
self.indexCount = (self.lastIndex - self.firstIndex)/7+1
print self.getVersion()," 纪录总数: %d 条 "%(self.indexCount)
def getVersion(self):
s = self.getIpAddr(0xffffff00L)
return s
def getAreaAddr(self,offset=0):
if offset :
self.ipdb.seek( offset )
str = self.ipdb.read( 1 )
(byte,) = struct.unpack('B',str)
if byte == 0x01 or byte == 0x02:
p = self.getLong3()
if p:
return self.getString( p )
else:
return ""
else:
self.ipdb.seek(-1,1)
return self.getString( offset )
def getAddr(self,offset,ip=0):
self.ipdb.seek( offset + 4)
countryAddr = ""
areaAddr = ""
str = self.ipdb.read( 1 )
(byte,) = struct.unpack('B',str)
if byte == 0x01:
countryOffset = self.getLong3()
self.ipdb.seek( countryOffset )
str = self.ipdb.read( 1 )
(b,) = struct.unpack('B',str)
if b == 0x02:
countryAddr = self.getString( self.getLong3() )
self.ipdb.seek( countryOffset + 4 )
else:
countryAddr = self.getString( countryOffset )
areaAddr = self.getAreaAddr()
elif byte == 0x02:
countryAddr = self.getString( self.getLong3() )
areaAddr = self.getAreaAddr( offset + 8 )
else:
countryAddr = self.getString( offset + 4 )
areaAddr = self.getAreaAddr()
return countryAddr + " " + areaAddr
def dump(self, first ,last ):
if last > self.indexCount :
last = self.indexCount
for index in range(first,last):
offset = self.firstIndex + index * 7
self.ipdb.seek( offset )
buf = self.ipdb.read( 7 )
(ip,of1,of2) = struct.unpack("IHB",buf)
print "%d\t%s\t%s" %(index, self.ip2str(ip), \
self.getAddr( of1 + (of2 << 16) ) )
def setIpRange(self,index):
offset = self.firstIndex + index * 7
self.ipdb.seek( offset )
buf = self.ipdb.read( 7 )
(self.curStartIp,of1,of2) = struct.unpack("IHB",buf)
self.curEndIpOffset = of1 + (of2 << 16)
self.ipdb.seek( self.curEndIpOffset )
buf = self.ipdb.read( 4 )
(self.curEndIp,) = struct.unpack("I",buf)
def getIpAddr(self,ip):
L = 0
R = self.indexCount - 1
while L < R-1:
M = (L + R) / 2
self.setIpRange(M)
if ip == self.curStartIp:
L = M
break
if ip > self.curStartIp:
L = M
else:
R = M
self.setIpRange( L )
#version information,255.255.255.X,urgy but useful
if ip&0xffffff00L == 0xffffff00L:
self.setIpRange( R )
if self.curStartIp <= ip <= self.curEndIp:
address = self.getAddr( self.curEndIpOffset )
else:
address = "未找到该IP的地址"
return address
def getIpRange(self,ip):
self.getIpAddr(ip)
range = self.ip2str(self.curStartIp) + ' - ' \
+ self.ip2str(self.curEndIp)
return range
def getString(self,offset = 0):
if offset :
self.ipdb.seek( offset )
str = ""
ch = self.ipdb.read( 1 )
(byte,) = struct.unpack('B',ch)
while byte != 0:
str = str + ch
ch = self.ipdb.read( 1 )
(byte,) = struct.unpack('B',ch)
return str
def ip2str(self,ip):
return str(ip>>24)+'.'+str((ip>>16)&0xffL)+'.' \
+str((ip>>8)&0xffL)+'.'+str(ip&0xffL)
def str2ip(self,s):
(ip,) = struct.unpack('L',socket.inet_aton(s))
return ((ip>>24)&0xffL)|((ip&0xffL)<<24) \
|((ip>>8)&0xff00L)|((ip&0xff00L)<<8)
def getLong3(self,offset = 0):
if offset :
self.ipdb.seek( offset )
str = self.ipdb.read(3)
(a,b) = struct.unpack('HB',str)
return (b << 16) + a
#Demo
def main():
IPL = IPLocator( "QQWry.dat" )
ip = ""
if len(sys.argv) != 2:
print 'Usage: python IPLocator.py <IP>'
return
else:
ip = sys.argv[1]
address = IPL.getIpAddr( IPL.str2ip(ip) )
range = IPL.getIpRange( IPL.str2ip(ip) )
print "此IP %s 属于 %s\n所在网段: %s" % (ip,address, range)
if __name__ == "__main__" :
main()
w
#conding utf-8
import re,fileinput
f=fileinput.input()
a=''
for i in f:
a=a+i
myre=re.compile(r'(\S+)\s+(\S+).*')
myr=myre.finditer(a)
for i in myr:
print (i.group(1),i.group(2))
#conding utf-8
import re,fileinput
f=fileinput.input()
a=''
for i in f:
a=a+i
myre=re.compile(r'(\S+)\s+(\S+).*')
myr=myre.findall(a)
a=[]
for i,l in myr:
k=re.findall(r'\d+\.\d+\.',i)
a.append(k)
del myr,myre,f,k,i,l
b=[]
for i in a:
i=i[0]+'0.0'
b.append((i,i[:-3]+'255.255'))
del a
b=list(set(b))
for i,l in b:
print (i,l)
a.txt
61.128.21.0 61.128.68.255 中国 电信
61.128.70.0 61.128.95.255 中国 电信
61.129.40.0 61.129.40.63 上海市 Intel中国上海分公司
61.130.6.224 61.130.6.255 浙江省杭州市 中国美术学院
61.131.11.132 61.131.11.132 福建省 中国石化分公司
61.133.193.168 61.133.193.168 宁夏银川市 猎人网吧(振西路中国银行对面)
61.137.127.51 61.137.127.51 湖南省长沙市 中国电信集团邮电规划设计院(湖南院)
61.138.94.10 61.138.94.10 内蒙古包头市 中国核工业集团公司二零二厂科宇网吧
61.138.99.13 61.138.99.13 内蒙古包头市 中国核工业集团公司二零二厂科宇网吧
61.138.217.45 61.138.217.45 云南省玉溪市通海县 南街中国电信大楼二楼新业务演示厅
61.138.253.254 61.138.253.254 新疆乌鲁木齐市 中国南航股份新疆分公司
61.139.44.116 61.139.44.116 四川省自贡市 中国电信自贡分公司(檀木林局)
61.139.64.5 61.139.64.5 四川省成都市 中国电信天府热线大楼
61.139.65.205 61.139.65.205 四川省成都市 新鸿路181号中国电信天府热线大楼
60.195.20.25 60.195.20.25 北京市 瑞得在线华宇时空网吧(中国戏曲学院十字路口)
60.205.129.0 60.205.129.255 中国 CZ88.NET
60.205.131.0 60.205.132.255 中国 CZ88.NET
219.225.81.0 219.225.95.255 北京市 中国农业大学
219.225.96.0 219.225.98.206 中国农业大学 东区3#楼本科男生
219.225.98.207 219.225.98.207 中国农业大学 东区3#楼本科男生502
219.225.98.208 219.225.100.13 中国农业大学 东区3#楼本科男生
219.225.100.14 219.225.100.14 中国农业大学 东区3#楼本科男生0628
219.225.100.15 219.225.100.223 中国农业大学 东区3#楼本科男生
你可以直接c:\test.py a.txt得到你想要的:
61.128.21.0 61.128.68.255
61.128.70.0 61.128.95.255
61.129.40.0 61.129.40.63
61.130.6.224 61.130.6.255
61.131.11.132 61.131.11.132
61.133.193.168 61.133.193.168
61.137.127.51 61.137.127.51
61.138.94.10 61.138.94.10
61.138.99.13 61.138.99.13
61.138.217.45 61.138.217.45
61.138.253.254 61.138.253.254
61.139.44.116 61.139.44.116
61.139.64.5 61.139.64.5
61.139.65.205 61.139.65.205
60.195.20.25 60.195.20.25
60.205.129.0 60.205.129.255
60.205.131.0 60.205.132.255
219.225.81.0 219.225.95.255
219.225.96.0 219.225.98.206
219.225.98.207 219.225.98.207
219.225.98.208 219.225.100.13
219.225.100.14 219.225.100.14
219.225.100.15 219.225.100.223