玩蛇网提供最新Python编程技术信息以及Python资源下载!
您现在的位置: 玩蛇网首页 > Python源码实例_Python程序源代码_网站项目下载 > 正文内容

用Python访问读取纯真IP数据库的方法代码

Python入门佳作 经典教程的全新修订 10个项目引人入胜
玩蛇网推荐图文教程:python黑客多线程扫描器

工作中我们常需要使用纯真IP数据库内的数据做分析,如何使用Python访问读取纯真IP数据库的方法有很多朋友需要。今天我自己写了个python代码,可以解决Python读取纯真IP数据库的需求,拿来和给大家借鉴一下。

我日常工作是用python版检索纯真IP数据库中的IP地址信息,以下代码大家可以自行修改再使用。

#!/usr/bin/python
 
from struct import *
import string
 
def ip2string( ip ):
    a = (ip & 0xff000000) >> 24
    b = (ip & 0x00ff0000) >> 16
    c = (ip & 0x0000ff00) >> 8
    d = ip & 0x000000ff
    return "%d.%d.%d.%d" % (a,b,c,d)
 
def string2ip( str ):
    ss = string.split(str, '.');
    ip = 0L

    for s in ss: ip = (ip << 8) + string.atoi(s)
    return ip;
 
class IpLocater :
    def __init__( self, ipdb_file ):
        self.ipdb = open( ipdb_file, "rb" )
        # get index address 
        str = self.ipdb.read( 8 )
        (self.first_index,self.last_index) = unpack('II',str)
        self.index_count = (self.last_index - self.first_index) / 7 + 1
 
    def getString(self,offset = 0):
        if offset :
            self.ipdb.seek( offset )

        str = ""
        ch = self.ipdb.read( 1 )
        (byte,) = unpack('B',ch)

        while byte != 0:
            str = str + ch
            ch = self.ipdb.read( 1 )
            (byte,) = unpack('B',ch) 
        return str
 
    def getLong3(self,offset = 0):
        if offset :
            self.ipdb.seek( offset )

        str = self.ipdb.read(3)
        (a,b) = unpack('HB',str)
        return (b << 16) + a
 
    def getAreaAddr(self,offset=0):
        if offset :
            self.ipdb.seek( offset )

        str = self.ipdb.read( 1 )
        (byte,) = unpack('B',str)

        if byte == 0x01 or byte == 0x02:
            p = self.getLong3()
            if p:
                return self.getString( p )
            else:#www.iplaypython.com
                return ""
        else:
            return self.getString( offset )
 
    def getAddr(self,offset ,ip = 0):
        self.ipdb.seek( offset + 4)
 
        countryAddr = ""
        areaAddr = ""
        str = self.ipdb.read( 1 )
        (byte,) = unpack('B',str)

        if byte == 0x01:
            countryOffset = self.getLong3()
            self.ipdb.seek(countryOffset )
            str = self.ipdb.read( 1 )
            (b,) = 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 output(self, first ,last ):
        if last > self.index_count :
            last = self.index_count

        for index in range(first,last):
            offset = self.first_index + index * 7
            self.ipdb.seek( offset )
            buf = self.ipdb.read( 7 )
            (ip,of1,of2) = unpack("IHB",buf)
            print "%s - %s" % (ip, self.getAddr( of1 + (of2 << 16) ) )
 
    def find(self,ip,left,right):
        if right-left == 1:
            return left
        else:
            middle = ( left + right ) / 2
            offset = self.first_index + middle * 7
            self.ipdb.seek( offset )
            buf = self.ipdb.read( 4 )
            (new_ip,) = unpack("I",buf)
            if ip <= new_ip :
                return self.find( ip, left, middle )
            else:
                return self.find( ip, middle, right )
 
    def getIpAddr(self,ip):
        index = self.find( ip,0,self.index_count - 1 )
        ioffset = self.first_index + index * 7
        aoffset = self.getLong3( ioffset + 4)
        address = self.getAddr( aoffset )
        return address                
 
if __name__ == "__main__" :
    ip_locater = IpLocater( "QQWry.Dat" )
    ip_locater.output( 100, 120 )
    ip = '59.64.234.174'
    ip = '58.38.139.229'
    address = ip_locater.getIpAddr( string2ip( ip ) )
    print "the ip %s come from %s" % (ip,address)

Python访问读取纯真IP数据库的方法代码一文中,关于Python string模块的使用方法,请参见:Python string模块 字符串函数方法操作教程

玩蛇网文章,转载请注明出处和来源网址:http://www.iplaypython.com/code/c2664.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网PythonQQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
出炉日期:2015-12-24 10:44 玩蛇网 www.iplaypython.com

我要分享到:
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules