5
27
2014
13

纯真 IP 数据库 QQWry 解析库 Python 3 版

本文来自依云's Blog,转载请注明。

这东西挺好用的,可惜我只寻到一多年以前的 Python 2 版本的,作者是 AutumnCat,不认识。但注释里提到的修改者 bones7456 是鼎鼎大名的骨头兄,现其博客已经长草……

一直以来,我都是通过子进程调用来使用的,因为我写的代码是 Python 3 版,比如这个寻找文本里的 IP 地址并标记的 ipmarkup 脚本。配合 Python 3.2 加入的 functools.lru_cache,效率还不错的样子。但近期有大量 IP 需要查询,才感到每个 IP 都开个子进程的方式实在太慢。遂将其修改为 Python 3 版,并加入了些 Python 后来才流行的 idiom。

脚本还是扔到 winterpy 仓库里了。GPLv2 授权的。

2014年8月2日更新:增加了在线更新的功能,从此不需要 Wine 就能更新数据库啦 :-) 更新方法来自微菜。更新命令如下:

python3 -m QQWry update
Category: python | Tags: python IP地址 | Read Count: 12805
MaskRay 说:
May 27, 2014 05:51:57 PM

和maxmind.com的GeoIP比怎麼樣?

Avatar_small
依云 说:
May 27, 2014 07:07:28 PM

那个不是只精确到国家的么?

MaskRay 说:
May 27, 2014 07:16:10 PM

% geoiplookup 1.2.4.8
GeoIP Country Edition: CN, China
GeoIP City Edition, Rev 1: CN, N/A, N/A, N/A, N/A, 35.000000, 105.000000, 0, 0
GeoIP ISP Edition: IP Address not found
GeoIP Organization Edition: China Network Information Center
GeoIP ASNum Edition: AS24151 China Internet Network Infomation Center
GeoIP Netspeed Edition: Unknown
GeoIP Domain Name Edition: IP Address not found
% geoiplookup 1.2.4.8
GeoIP Country Edition: CN, China
GeoIP City Edition, Rev 1: CN, N/A, N/A, N/A, N/A, 35.000000, 105.000000, 0, 0
GeoIP ISP Edition: IP Address not found
GeoIP Organization Edition: China Network Information Center
GeoIP ASNum Edition: AS24151 China Internet Network Infomation Center
GeoIP Netspeed Edition: Unknown
GeoIP Domain Name Edition: IP Address not found

公開的http ftp列目錄站點能查到
GeoIPASNum.dat GeoIPCity.dat GeoIP.dat GeoIPDomain.dat GeoIPISP.dat GeoIPNetSpeed.dat GeoIPOrg.dat GeoIPv6.dat

Avatar_small
依云 说:
May 27, 2014 07:30:09 PM

有些是收费的呢。

StarBrilliant 说:
May 27, 2014 08:55:22 PM

为什么不把纯真数据库提前转换成别的数据库格式来加快查询?

Avatar_small
依云 说:
May 27, 2014 09:57:23 PM

会更快么?现在的速度已经可以接受了,就不花那工夫了。

StarBrilliant 说:
May 27, 2014 11:26:51 PM

请告诉我你每做一次查询fork了多少次。

Avatar_small
依云 说:
May 28, 2014 09:55:53 AM

现在已经不需要调子进程了啦。

zjz311 说:
May 30, 2014 05:22:03 PM

我在知乎上看到 @vczh 这样回答的:
读qqwy.bat生成B+树保存成一个新文件,可以优化硬盘IO的次数
链接传送门:http://www.zhihu.com/question/21811660

MaskRay 说:
May 30, 2014 10:15:08 PM

這樣說的話 trie 類的數據結構比如 crit-bit tree 空間效率會比 B+ tree 更好……

maxmind.com 的數據庫確實要錢……網上根據文件名可以搜索到舊的

maplebeats 说:
May 30, 2014 10:18:48 PM

哈哈,我们内部有个IP库,准确度挺不错的。。。广泛使用的:D

Avatar_small
依云 说:
May 31, 2014 11:51:43 AM

那人怎么花了那么久……我这里查一个地址才 118µs 左右呢。

CNLIC 说:
Jan 12, 2015 03:43:27 AM

你那有更新的吗?谢谢

root@do:~# ls -l /usr/share/GeoIP/
total 108176
-rw-r--r-- 1 root root 756904 Jan 7 03:13 GeoIP.dat
-rw-r--r-- 1 root root 3792589 Jan 5 14:06 GeoIPASNum.dat
-rw-r--r-- 1 root root 50585010 Jan 11 19:15 GeoIPCity.dat
-rw-r--r-- 1 root root 4805774 Jan 11 19:16 GeoIPDomain.dat
-rw-r--r-- 1 root root 2912896 Jan 11 19:17 GeoIPISP.dat
-rw-r--r-- 1 root root 7258766 Jan 11 19:17 GeoIPNetSpeedCell.dat
-rw-r--r-- 1 root root 31968222 Jan 11 19:17 GeoIPOrg.dat
-rw-r--r-- 1 root root 8675929 Jan 11 15:15 GeoIPRegion.dat
root@do:~# geoiplookup google.com -v
GeoIP Country Edition: GEO-106FREE 20150106 Build 1 Copyright (c) 2015 MaxMind Inc All Rights Reserved
GeoIP City Edition, Rev 1: GEO-133 20140909 Build 1 Copyright (c) 2014 MaxMind Inc All Rights Reserved
GeoIP Region Edition, Rev 1: GEO-115 20110816 Build 1 Copyright (c) 2011 MaxMind Inc All Rights Reserved
GeoIP ISP Edition: GEO-121 20140902 Build 1 Copyright (c) 2014 MaxMind Inc All Rights Reserved
GeoIP Organization Edition: GEO-111 20131203 Build 1 Copyright (c) 2013 MaxMind Inc All Rights Reserved
GeoIP City Edition, Rev 0: GEO-133 20140909 Build 1 Copyright (c) 2014 MaxMind Inc All Rights Reserved
GeoIP Region Edition, Rev 0: GEO-115 20110816 Build 1 Copyright (c) 2011 MaxMind Inc All Rights Reserved
GeoIP ASNum Edition: GEO-117 20150105 Build 1 Copyright (c) 2015 MaxMind Inc All Rights Reserved
GeoIP Domain Name Edition: GEO-173 20120904 Build 1 Copyright (c) 2012 MaxMind Inc All Rights Reserved
GeoIP Large Country Edition: GEO-106FREE 20150106 Build 1 Copyright (c) 2015 MaxMind Inc All Rights Reserved


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

| Theme: Aeros 2.0 by TheBuckmaker.com