tinyipdate最新ip库 python操作类,tinyipdatepython,发布最新版本tinyip


发布最新版本tinyipdata.dat ip库, Discuz的程序可以直接下载tinyipdata.dat替换现有的文件吧。 包含了UTF-8 和GBK 两个版本文件。

https://github.com/tuanv/ipdata.git

数据结果:

ip: 183.25.245.83 , area: - 广东省惠州市电信

ip: 124.73.44.205 , area: - 安徽省合肥市电信

ip: 72.170.236.5 , area: - 美国联合航空通信网络(西南部通用)

ip: 22.80.66.70 , area: - 美国DoD网络信息中心

ip: 136.55.110.12 , area: - 美国密歇根州福特汽车公司

ip: 98.138.147.65 , area: - 美国雅虎公司

ip: 124.93.15.125 , area: - 辽宁省大连市联通ADSL

ip: 120.79.17.33 , area: - 中国长城宽带

ip: 138.100.238.150 , area: - 西班牙马德里科技大学

ip: 112.206.108.211 , area: - 菲律宾PLDT通讯公司

ip: 123.160.87.196 , area: - 河南省郑州市电信ADSL

ip: 116.209.27.158 , area: - 湖北省仙桃市电信

ip: 23.34.250.65 , area: - 美国Akamai科技公司CDN网络节点

#!/usr/bin/env python# coding: utf-8#from __future__ import with_statementimport re, socket, sysfrom struct import pack,unpackclass ipdata():    def __init__(self):        self.ipdatafile = './tinyipdata.dat'        self.re = '';    def setipdatafile(self,file):        self.ipdatafile = file    def ip2long(self,ip):        return unpack("!I", socket.inet_aton(ip))[0]    def convertip(self,ip):        pat = re.compile("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}")        if pat.match(ip):            ipdot = ip.split('.')            #            if int(ipdot[0]) == 10 or int(ipdot[0]) == 127 or (int(ipdot[0]) == 192 and int(ipdot[1]) == 168) or (int(ipdot[0]) == 172 and (int(ipdot[1]) >= 16 and int(ipdot[1]) <= 31)):                self.re = '- LAN'            elif int(ipdot[0]) > 255 or int(ipdot[1]) > 255 or int(ipdot[2]) > 255 or int(ipdot[3]) > 255 :                self.re = '- Invalid IP Address'            else :                fp = None ; offset = 0 ;index = None ; index_length = index_offset = 0                ip = pack('>i',self.ip2long(ip))                ipdot[0] = int(ipdot[0])                ipdot[1] = int(ipdot[1])                if fp == None :                    try:                        fp = open(self.ipdatafile, 'rb')                    except Exception, e:                        return '- Invalid IP data file'                    offset = unpack('>i', fp.read(4))[0]                    index = fp.read(offset - 4)                    length = offset - 1028;                    start = unpack('<i', index[ipdot[0] * 4] + index[ipdot[0] * 4 + 1] + index[ipdot[0] * 4 + 2] + index[ipdot[0] * 4 + 3])[0]                    start = start * 8 + 1024                    while start < length :                        if index[start] + index[start + 1] + index[start + 2] + index[start + 3] >= ip :                            index_offset = unpack('<i', index[start + 4] + index[start + 5] + index[start + 6] + '\\x00')[0]                            index_length = unpack('B', index[start + 7])[0]                            break                        else :                            start = start+ 8                            continue                    fp.seek(offset + index_offset - 1024)                    if index_length > 0 :                        return '- ' + fp.read(index_length)                    else :                        return '- UnKnown'        return self.reif __name__ == '__main__':    path = sys.path[0]    file = path[:-11]+'tinyipdata.dat'    ip = '42.242.160.0'    ipdata = ipdata()    ipdata.setipdatafile(file)    print 'ip %s => %s' %(ip,ipdata.convertip(ip))#该片段来自于http://byrx.net

评论关闭