您的位置:首页 > 编程语言 > Python开发

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.gz

http://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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: