GeoIP的详解 --Python版
2016-01-22 10:02
701 查看
GeoIP
GeoIP数据库(MaxMind公司)可以根据来访者的IP,定位他的经纬度,国家/地区,省市,甚至街道等位置信息
本人用Python写GeoIP的API,就以Python版为例详细介绍一下GeoIP的用法
安装pygeoip
1.下载pygeoip安装包
解压安装(也可以通过命令行 pip install pygeoip):
下载安装包 https://github.com/appliedsec/pygeoip 解压后进入pygeoip-master目录运行 Python setup.py install
如下图:
2.下载Country,City库
http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gzhttp://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
将解压后的库复制到pygeoip-master目录中
3.测试
Python脚本写GeoIP接口
#-*- encoding: utf-8 -*-import sys
sys.path.append("..")
reload(sys)
sys.setdefaultencoding('utf-8')
import pygeoip
##输入IP,取得对应经纬度
class PygeoIp(object):
def __init__(self):
super(PygeoIp, self).__init__()
self.gi = pygeoip.GeoIP('C:\pygeoip-master\GeoLiteCity.dat',
pygeoip.MEMORY_CACHE)
#self.gi = pygeoip.GeoIP('C:\pygeoip-master\GeoIP.dat', pygeoip.MEMORY_CACHE)
def ip(self,value):
#location = self.gi.country_code_by_addr(value)
location = self.gi.record_by_addr(value)
print location.get("latitude"),location.get("longitude")
if __name__ == '__main__':
reparser = PygeoIp()
reparser.ip("8.8.8.8")
结果
GeoIP一些相关网址
http://dev.maxmind.com/geoip/legacy/install/country/http://dev.maxmind.com/geoip/legacy/downloadable/
https://github.com/maxmind/geoip-api-python
相关文章推荐
- python 学习笔记4
- Python 字符串操作方法大全
- Python 列表排序方法reverse、sort、sorted详解
- Python相关常用库概念介绍
- python 编程 规范
- Python 列表排序方法reverse、sort、sorted操作方法
- Python练习_更改配置文件(3)
- On Mac platform,Using Eclipse + PyDev Plugins + Python to build a Python development environment
- pxssh交换机自动刷限速模板
- Flask Web平台搭建
- python之haproxy配置文件操作(第三天)
- opencv for python学习一之保存视频
- Python 列表排序方法reverse、sort、sorted详解
- Python爬虫抓取手机APP的传输数据
- Python爬虫模拟登录带验证码网站
- Python Singleton
- 2016/01/22 Python学习3
- Python日期时间操作学习
- [Python]使用中的一些小方法集合
- Python学习第五天