GeoIP 分为商业版和免费版,免费版比商业版精度差了许多,经测试关于城市定位的确有差距,能否承受看你的精度请求!
一、免费版本引见:
1、GeoLite 版本,网上传播较广,类型为 dat 格式文件,库文件较小未停止精准度测试。
2、GeoLite2版本,目前最新版本,文件为 mmdb 格式文件,有兴味理解 mmdb 格式的点这里 。mmdb
两者文件大小比对,GeoLite2 特性点击这里
$ du -sh * 32M GeoLite2-City.mmdb 2.3M GeoLite2-Country.mmdb 18M GeoLiteCity.dat 732K GeoLiteCountry.datCity 文件为包含城市信息,Country 文件为国度信息。
二、下载 GeoLite2
下载方式分为两种,第一种经过下载 gz 紧缩包,第二种经过运用官方提供的下载更新程序,倡议运用第二种,官方称在每个月的第一个星期二更新,假如想做成方案任务每月都更新倡议选择第二种!GeoIP2细致更新日志点这里。
两种方式这里都啰嗦一下,本阶段只是讲如何下载,调用方式需求参考第三阶段 API 调用局部!
1、第一种方式,下载 gz 文件并解紧缩。
GeoLite2 只提供 City 和 Country 下载 查看详情点击里,文件分为 Binary 和 CVS 两种,这里运用 Binary 文件。
$ sudo mkdir -p /mnt/data/geolite2 && cd $_ $ sudo wget http://geolite.maxmind/download/geoip/database/GeoLite2-City.mmdb.gz $ sudo gzip -d GeoLite2-City.mmdb.gz $ sudo wget http://geolite.maxmind/download/geoip/database/GeoLite2-Country.mmdb.gz $ sudo gzip -d GeoLite2-Country.mmdb.gz
2、第二种方式,需安装官方下载更新程序 geoipupdate 。
a、到 GitHub下载地址 下载 geoipupdate,目前最新版为 2.1.0,GitHub 衔接速度要有耐烦,肯定能够下载滴!编译文件需求 libcurl-devel 包支持,需提早下载安装。
$ sudo yum install libcurl-devel -y $ sudo wget https://github/maxmind/geoipupdate/releases/download/v2.1.0/geoipupdate-2.1.0.tar.gz $ sudo tar xzvf geoipupdate-2.1.0.tar.gz $ cd geoipupdate-2.1.0 $ sudo ./configure $ sudo make $ sudo make install
编译终了只需求关注两个文件
更新执行文件 /usr/local/bin/geoipupdate
账户信息文件 /usr/local/etc/GeoIP.conf
b、配置账户信息 GeoIP.conf,修正配置文件如下即可,本配置文件默许下载 mmdb 文件,若想下载 dat 文件取消注释即可!
# The following UserId and LicenseKey are required placeholders: UserId 999999 LicenseKey 000000000000 # Include one or more of the following ProductIds: # * GeoLite2-City - GeoLite 2 City # * GeoLite2-Country - GeoLite2 Country # * 506 - GeoLite Legacy Country # * 517 - GeoLite Legacy ASN # * 533 - GeoLite Legacy City # dat 格式 #ProductIds GeoLite2-City GeoLite2-Country 506 533 # mmdb 格式 ProductIds GeoLite2-City GeoLite2-Country 132 106
c、执行更新
查看geoipupdate协助文件,理解有哪些参数能够运用! -d 参数将文件下载到指定目录,-v 参数就是显现下载过程明细信息。
$ /usr/local/bin/geoipupdate -h Usage: geoipupdate [-Vhv] [-f license_file] [-d custom directory] -d DIR store downloaded files in DIR -f FILE use configuration found in FILE (see GeoIP.conf(5) man page) -h display this help text -v use verbose output -V display the version and exit
执行更新命令,看网络状况,本文将文件下载到 /mnt/data/geolite2/目录 。
$ ll /mnt/data/geolite2/ && cd $_ 总用量 0 $ sudo /usr/local/bin/geoipupdate -d /mnt/data/geolite2/ -v $ ll 总用量 34088 -rw-r--r--. 1 root root 32553611 12月 19 18:14 GeoLite2-City.mmdb -rw-r--r--. 1 root root 2349406 12月 19 18:14 GeoLite2-Country.mmdb
如何配置方案任务定时更新 GeoLite2 请自行处理。
三、安装 GeoLite2 API 调用程序
官方提供 (C#)、C、Java、Perl、Python、Apache API调用。其他第三方接口也有,但官方不提供技术支持,详情点击这里。
本文运用 C 言语API接口停止调用测试,其他言语请参考官方指导自行处理!C言语API GitHub 下载地址
$ sudo wget https://github/maxmind/libmaxminddb/releases/download/1.0.3/libmaxminddb-1.0.3.tar.gz $ sudo tar xzvf libmaxminddb-1.0.3.tar.gz $ cd libmaxminddb-1.0.3 $ sudo ./configure $ sudo make $ sudo make install
查看协助文档
$ /usr/local/bin/mmdblookup --help
mmdblookup --file /path/to/file.mmdb --ip 1.2.3.4 [path to lookup]
This application accepts the following options:
--file (-f) The path to the MMDB file. Required.
--ip (-i) The IP address to look up. Required.
--verbose (-v) Turns on verbose output. Specifically, this causes this
application to output the database metadata.
--version Print the programs version number and exit.
--help (-h -?) Show usage information.
If an IPs data entry resolves to a map or array, you can provide
a lookup path to only show part of that data.
For example, given a JSON structure like this:
{
"names": {
"en": "Germany",
"de": "Deutschland"
},
"cities": [ "Berlin", "Frankfurt" ]
}
You could look up just the English name by calling mmdblookup with a lookup path of:
mmdblookup --file ... --ip ... names en
Or you could look up the second city in the list with:
mmdblookup --file ... --ip ... cities 1
Array numbering begins with zero (0).
If you do not provide a path to lookup, all of the information for a given IP
will be shown.四、测试
测试IP:112.225.35.70 山东省青岛市 联通
1、获取国度信息,国度信息是正确滴,看着像乱码的是显现的俄语!
$ /usr/local/bin/mmdblookup --file /mnt/data/geolite2/GeoLite2-Country.mmdb --ip 112.225.35.70
{
"continent":
{
"code":
"AS"
"geoname_id":
6255147
"names":
{
"de":
"Asien"
"en":
"Asia"
"es":
"Asia"
"fr":
"Asie"
"ja":
"アジア"
"pt-BR":
"Ásia"
"ru":
"Азия"
"zh-CN":
"亚洲"
}
}
"country":
{
"geoname_id":
1814991
"iso_code":
"CN"
"names":
{
"de":
"China"
"en":
"China"
"es":
"China"
"fr":
"Chine"
"ja":
"中国"
"pt-BR":
"China"
"ru":
"Китай"
"zh-CN":
"中国"
}
}
"registered_country":
{
"geoname_id":
1814991
"iso_code":
"CN"
"names":
{
"de":
"China"
"en":
"China"
"es":
"China"
"fr":
"Chine"
"ja":
"中国"
"pt-BR":
"China"
"ru":
"Китай"
"zh-CN":
"中国"
}
}
}
2、获取城市信息,这个数据就有点纠结了,省份没有问题,城市是有问题的! 官方演示地址 十分精准,或许这就是免费和收费的差异 :)
$ /usr/local/bin/mmdblookup --file /mnt/data/geolite2/GeoLite2-City.mmdb --ip 112.225.35.70
{
"city":
{
"geoname_id":
1805753
"names":
{
"de":
"Jinan"
"en":
"Jinan"
"es":
"Jinan"
"fr":
"Jinan"
"ja":
"済南市"
"pt-BR":
"Jinan"
"ru":
"Цзинань"
"zh-CN":
"济南"
}
}
"continent":
{
"code":
"AS"
"geoname_id":
6255147
"names":
{
"de":
"Asien"
"en":
"Asia"
"es":
"Asia"
"fr":
"Asie"
"ja":
"アジア"
"pt-BR":
"Ásia"
"ru":
"Азия"
"zh-CN":
"亚洲"
}
}
"country":
{
"geoname_id":
1814991
"iso_code":
"CN"
"names":
{
"de":
"China"
"en":
"China"
"es":
"China"
"fr":
"Chine"
"ja":
"中国"
"pt-BR":
"China"
"ru":
"Китай"
"zh-CN":
"中国"
}
}
"location":
{
"latitude":
36.668300
"longitude":
116.997200
"time_zone":
"Asia/Shanghai"
}
"registered_country":
{
"geoname_id":
1814991
"iso_code":
"CN"
"names":
{
"de":
"China"
"en":
"China"
"es":
"China"
"fr":
"Chine"
"ja":
"中国"
"pt-BR":
"China"
"ru":
"Китай"
"zh-CN":
"中国"
}
}
"subdivisions":
[
{
"geoname_id":
1796328
"iso_code":
"37"
"names":
{
"en":
"Shandong Sheng"
"zh-CN":
"山东省"
}
}
]
}
由于获取的数据是 Json 格式,所以依据协助文档提示能够对内容停止格式化输出,如输出城市中 city->names->zh-CN 内容
$ /usr/local/bin/mmdblookup --file /mnt/data/geolite2/GeoLite2-City.mmdb --ip 112.225.35.70 city names zh-CN "济南"
固然 GeoIP2 在城市定位剖析的不是很理想,但对我来说精度能够承受,聊胜于无嘛!
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-27537-1.html
简直各种感动各种正力量好不好