GeoIP数据库(MaxMind公司)可以根据来访者的IP, 定位他的经纬度,国家/地区,省市,甚至街道等位置信息
本人用Python写GeoIP的API,就以Python版为例详细介绍一下GeoIP的用法
下载安装包 https://github.com/appliedsec/pygeoip
解压后进入pygeoip-master目录运行 Python setup.py install
如下图:
http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
将解压后的库复制到pygeoip-master目录中
#-*- 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")
http://dev.maxmind.com/geoip/legacy/install/country/
http://dev.maxmind.com/geoip/legacy/downloadable/
https://github.com/maxmind/geoip-api-python