Python读取纯真IP数据库
转⾄:blog.51cto/qicheng0211/1589442
⼀、获取最新版IP地址数据库qqwry.dat
纯真IP地址数据库下载地址:
在windows机器上下载解压,点击安装,在安装⽬录下的qqwry.dat即是最新版ip数据库。
也可从51CTO下载(不是最新版,可⽤于测试):
⼆、IPLocator.py
⽹上到别⼈⽤Python写的纯真IP数据库的查询程序,原⽂地址:,本⽂对代码做了⼀些修改,解决中⽂乱码问题。建⽴IPLocator.py⽂件(见本⽂附件),内容如下:适⽤于python2
#! /usr/bin/env python
# -*- coding: utf-8 -*-
""" IPLocator: locate IP in the QQWry.dat.
Usage:
python IPLocator.py <ip>
"""
import socket,string,struct,sys
class IPLocator :
def __init__( self, ipdbFile ):
self.ipdb = open( ipdbFile, "rb" )
str = ad( 8 )
(self.firstIndex,self.lastIndex) = struct.unpack('II',str)
self.indexCount = (self.lastIndex - self.firstIndex)/7+1
Version()," 纪录总数: %d 条 "%(self.indexCount)
def getVersion(self):
s = IpAddr(0xffffff00L)
return s
def getAreaAddr(self,offset=0):
if offset :
self.ipdb.seek( offset )
str = ad( 1 )
(byte,) = struct.unpack('B',str)
if byte == 0x01 or byte == 0x02:
p = Long3()
if p:
String( p )
else:
return ""
else:
self.ipdb.seek(-1,1)
String( offset )
def getAddr(self,offset,ip=0):
self.ipdb.seek( offset + 4)
countryAddr = ""
areaAddr = ""
str = ad( 1 )
(byte,) = struct.unpack('B',str)
if byte == 0x01:
countryOffset = Long3()
self.ipdb.seek( countryOffset )
str = ad( 1 )
(b,) = struct.unpack('B',str)
if b == 0x02:
countryAddr = String( Long3() )
self.ipdb.seek( countryOffset + 4 )
else:
countryAddr = String( countryOffset )
areaAddr = AreaAddr()
elif byte == 0x02:
countryAddr = String( Long3() )
areaAddr = AreaAddr( offset + 8 )
else:
countryAddr = String( offset + 4 )
areaAddr = AreaAddr()
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 = ad( 7 )
(ip,of1,of2) = struct.unpack("IHB",buf)
address = Addr( of1 + (of2 << 16) )
#把GBK转为utf-8
address = unicode(address,'gbk').encode("utf-8")            print "%d\t%s\t%s" %(index, self.ip2str(ip), \
address )
def setIpRange(self,index):
offset = self.firstIndex + index * 7
self.ipdb.seek( offset )
python怎么读取dat文件buf = ad( 7 )
(self.curStartIp,of1,of2) = struct.unpack("IHB",buf)        self.curEndIpOffset = of1 + (of2 << 16)
self.ipdb.seek( self.curEndIpOffset )
buf = ad( 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 = Addr( self.curEndIpOffset )
#把GBK转为utf-8
address = unicode(address,'gbk').encode("utf-8")        else:
address = "未到该IP的地址"
return address
def getIpRange(self,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 = ad( 1 )
(byte,) = struct.unpack('B',ch)
while byte != 0:
str = str + ch
ch = ad( 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('I',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 = ad(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 = IpAddr( IPL.str2ip(ip) )
range = IpRange( IPL.str2ip(ip) )
print "此IP %s 属于 %s\n所在⽹段: %s" % (ip,address, range)
if __name__ == "__main__" :
main()
  把qqwry.dat放到和IPLocator.py同⼀⽬录,使⽤⽅法如下(请参考IPLocator.py中的Demo):

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。