Python 安装 MaxMind GeoLite City

1、先安装 geoip c library
 geoip c library >= 1.4.6 installed on your machine. >= 1.4.6 installed on your machine.
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.7.tar.gz
tar -xvzf GeoIP-1.4.7.tar.gz
cd GeoIP-1.4.7
./confiure
make 
make install 
2、下载GeoLite City Phthon版本
https://codeload.github.com/maxmind/geoip-api-python/zip/master
unzip master
$ python setup.py build
$ python setup.py install

3、下载GeoLiteCity.dat
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz 
gunzip GeoLiteCity.dat.gz 
4、用法
>>> import GeoIP
>>> geodb='/home/maxMind/GeoLiteCity.dat'
>>> gi = GeoIP.open(geodb, GeoIP.GEOIP_STANDARD)                                      
>>> ip='180.76.1.3'
>>> gi.record_by_addr(ip)
{'city': 'Beijing', 'region_name': 'Beijing', 'region': '22', 'area_code': 0, 'time_zone': 'Asia/Harbin', 'longitude': 116.38829803466797, 'metro_code': 0, 'country_code3': 'CHN', 'latitude': 39.92890167236328, 'postal_code': None, 'dma_code': 0, 'country_code': 'CN', 'country_name': 'China'}
>>> gir = gi.record_by_name("www.google.com")
>>> gir
{'city': 'Mountain View', 'region_name': 'California', 'region': 'CA', 'area_code': 650, 'time_zone': 'America/Los_Angeles', 'longitude': -122.05740356445312, 'metro_code': 807, 'country_code3': 'USA', 'latitude': 37.4192008972168, 'postal_code': '94043', 'dma_code': 807, 'country_code': 'US', 'country_name': 'United States'}
>>> 
   
   
if gir is not None:
     print(gir[ 'country_code'])
     print(gir[ 'country_code3'])
     print(gir[ 'country_name'])
     print(gir[ 'city'])
     print(gir[ 'region'])
     print(gir[ 'region_name'])
     print(gir[ 'postal_code'])
     print(gir[ 'latitude'])
     print(gir[ 'longitude'])
     print(gir[ 'area_code'])
     print(gir[ 'time_zone'])
     print(gir[ 'metro_code'])
     print( str(gir))
>>> gir['country_code'],gir['city'],gir['region_name']
('US', 'Mountain View', 'California')
>>>

注意: 安装完成后,最好执行一次   /sbin/ldconfig,否则在import GeoIP时,可能会报:
“ libGeoIP.so.1: cannot open shared object file: No such file or directory"
默认 libGeoIP.so.1的路径是 /usr/local/lib/libGeoIP.so.1,需要检查/etc/ld.so.conf文件中是否有包含:
/usr/local/lib
若没有,则加上此目录再 /sbin/ldconfig。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值