使用 GeoIP2 获取 IP 的地理位置

简介:

 准备工作

准备好这两个就可以了。


 2. 敲代码

我使用的操作系统是 CentOS7.1。

2.1 编译 libmaxminddb
把 libmaxminddb 的代码下载下来,随便放在什么地方, 然后解压,进入解压后的目录,执行 bootstrap,configure,make

复制代码
$ tar xzf libmaxminddb-1.3.1.tar.gz
$ cd libmaxminddb-1.3.1
$ ./bootstrap
$ ./configure
$ make
复制代码

1.在执行 bootstrap 的时候可能会报错,应该是缺少 autoconf 之类的自动构建工具导致的,到谷歌或者百度查询一下安装即可。

2.在执行 make 命令的时候,也会报错,不过报错信息指向的是 libmaxminddb-1.3.1/t 里面缺少 libtap/tap.h。 https://github.com/pozorvlak/libtap 是 libtap 在 GitHub 上的地址, 其项目首页上的注释是:Testing library for C, implementing the Test Anything Protocol. Written by Nik Clayton.

既然是测试使用的,那么不编译应该也没什么大问题。

3.看 make 命令后的编译记录,可以发现 libmaxminddb.a 静态链接库已经编译好了, 并且放在了 libmaxminddb-1.3.1/src/.libs 目录里面。

复制代码
[root@fengbo libmaxminddb-1.3.1]# ls src/.libs/ -l
total 312
-rw-r--r--. 1 root root  12632 Dec  8 22:30 data-pool.o
-rw-r--r--. 1 root root 111620 Dec  8 22:30 libmaxminddb.a
lrwxrwxrwx. 1 root root     18 Dec  8 22:30 libmaxminddb.la -> ../libmaxminddb.la
-rw-r--r--. 1 root root    959 Dec  8 22:30 libmaxminddb.lai
lrwxrwxrwx. 1 root root     21 Dec  8 22:30 libmaxminddb.so -> libmaxminddb.so.0.0.7
lrwxrwxrwx. 1 root root     21 Dec  8 22:30 libmaxminddb.so.0 -> libmaxminddb.so.0.0.7
-rwxr-xr-x. 1 root root  79477 Dec  8 22:30 libmaxminddb.so.0.0.7
-rw-r--r--. 1 root root 100104 Dec  8 22:30 maxminddb.o
复制代码

2.2 编写示例代码

示例代码是从 maxmind 的官方 GitHub 上直接复制下来的。不过我加了几条日志信息。 https://github.com/maxmind/libmaxminddb/blob/master/doc/libmaxminddb.md#example

代码如下:$cat example.c

  View Code

编译我们的示例代码:

把 libmaxminddb 源码中的 libmaxminddb-1.3.1/include/maxminddb_config.h 和 libmaxminddb-1.3.1/include/maxminddb.h 放到 example.c 所在的目录下。 还有 libmaxminddb-1.3.1/src/.libs/libmaxminddb.a 也要放进来。

复制代码
$ gcc -o example example.c ./libmaxminddb.a
$ ls 
example  example.c libmaxminddb.a  maxminddb_config.h  maxminddb.h
复制代码

2.3 下载 GeoLite2 开源数据库
到网页 https://dev.maxmind.com/geoip/geoip2/geolite2/ 中,下载 GeoLite2 的数据库。

Downloads

Database MaxMind DB binary, gzipped CSV format, zipped
GeoLite2 City Download (md5 checksum) Download (md5 checksum)
GeoLite2 Country Download (md5 checksum) Download (md5 checksum)
GeoLite2 ASN (Autonomous System Number) Download (md5 checksum) Download (md5 checksum)

我下载的是 GeoLite2 City 数据库。解压后的文件如下:

[root@fengbo maxmind]# ls GeoLite2-City_20171205/
COPYRIGHT.txt  GeoLite2-City.mmdb  LICENSE.txt  README.txt

2.4 测试一下这个程序的效果
先拿到一个 IP,比如 www.fengbohello.top 的 IP。

[root@fengbo maxmind]$ ping www.fengbohello.top
PING www.fengbohello.top (139.199.212.133) 56(84) bytes of data.
64 bytes from 139.199.212.133: icmp_seq=1 ttl=48 time=41.7 ms

运行一下 example 试试看:

  View Code

我擦,解析结果居然是北京。我问了十三哥,他说我们的服务器是腾讯云的,当时选择的是深圳机房。

下面是使用不同的厂商得到的结果:

厂商 地理位置
百度 广东省广州市 腾讯集团
ip.cn 广东省深圳市 腾讯云
freegeoip.net China.Beijing
新浪:int.dpool.sina.com.cn 中国.广东.广州
淘宝:ip.taobao.com 中国.华南.广东省.广州市.电信
腾讯:ip.qq.com 中国广东省广州市 未知

有意思吧,我也不知道该信谁的。



本文转自郝峰波博客园博客,原文链接:http://www.cnblogs.com/fengbohello/p/8144788.html,如需转载请自行联系原作者

相关文章
|
6月前
|
存储 JSON Cloud Native
C++ QT获取本机公网IP和IP所在地
C++ QT获取本机公网IP和IP所在地
|
3小时前
|
安全 网络安全
anywhere 无法正常使用的问题--IP地址解析
anywhere 无法正常使用的问题--IP地址解析
13 0
|
9月前
|
Linux 网络安全 API
Python通过GeoIP获取IP信息(国家、城市、经纬度等)
IP地址信息是非常重要的情报信息,通过IP可以定位到该IP所在的国家、城市、经纬度等。 获取IP信息的方式有很多,很多服务商都提供了相应的地址库或API接口服务。 如国内的ipip.net,国外的ip-api.com、maxmind.com等。 很多公司都是使用Maxmind网站的IP信息库,里面包含着IP的详细信息,有付费的也有免费的,收费与免费的区别就是精准度和覆盖率。 本文介绍下载及定时更新Maxmind的离线库用python通过GeoIP来获取IP信息
362 0
|
9月前
|
Java 定位技术 数据库
通过ip获取地理位置信息
通过ip获取地理位置信息
377 0
|
9月前
|
Kubernetes 网络协议 应用服务中间件
k8s场景测试之ingresss中geoip的使用
Geo是geographic的缩写,意思是地理的,GeoIP即为IP地理位置数据库,可以根据IP获得地理位置信息。GeoLite2是GeoIP2的免费版本,与GeoIP2数据库相比准确性较差。 GeoIP库可以根据IP地址(支持IPv4 和 IPv6), 定位该IP所在的 洲、经纬度、国家、省市、ASN 等信息。
207 0
|
应用服务中间件 定位技术 网络安全
Nginx访问日志接入GrayLog4.2.5并通过GeoIP展现访问者IP的地理位置信息
Nginx访问日志接入GrayLog4.2.5并通过GeoIP展现访问者IP的地理位置信息
411 0
Nginx访问日志接入GrayLog4.2.5并通过GeoIP展现访问者IP的地理位置信息
|
网络协议 定位技术 API
IP地理位置库开通服务解析
IP地理位置库是云解析DNS下的子产品服务,该服务为用户提供免费的页面在线查询IP地理位置功能,但是若需要通过API接口方式查询IP地址地理定位信息,则需要开通相关付费服务,本文主要介绍如何开通付费产品使用API来查询IP地址地理位置信息。
2852 0