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. C#线程池使用
  2. 使用gulp-connect实现web服务器
  3. C#操作Mongodb的心得
  4. 阅读笔记 火球——UML大战需求分析 2
  5. --查询nvarchar(max)的表和字段
  6. JavaScript的角色巨变和Web技术的发展
  7. JavaScript网站设计实践(五)编写photos.html页面,实现点击缩略图显示大图的效果
  8. 【 NOIP2015 DAY1 T2 信息传递】带权并查集
  9. jquery 获取元素的 实际宽度和高度
  10. S2SH框架集成详解(Struts 2.3.16 + Spring 3.2.6 + Hibernate 3.6.10)
  11. 面向对象程序设计-C++_课时22向上造型
  12. awk与sed:关于多行的样本
  13. CSS样式类型
  14. xml的SAX解析和dom解析的区别
  15. BZOJ1565 植物大战僵尸 题解
  16. [BZOJ]4199 品酒大会(Noi2015)
  17. Docker 配置国内镜像加速器,加速下载速度
  18. Spring Boot 2.x基础教程:工程结构推荐
  19. [译]async/await中阻塞死锁
  20. (二)Qt窗口应用程序Widget

热门文章

  1. flask 开发环境搭建
  2. JAVA常见算法题(二十七)
  3. 通过python脚本查看端口
  4. python pip更换下载源(转)
  5. http://www.360doc.com/content/12/0516/14/1671317_211422841.shtml
  6. angular directive 深入理解
  7. B7:访问者模式 Visitor
  8. Python学习笔记(三)多线程的使用
  9. 【React Native开发】React Native控件之Image组件解说与美团首页顶部效果实例(10)
  10. <十>读<<大话设计模式>>之观察者模式