原文: http://www.cnblogs.com/wangxiaoqiangs/p/6406087.html
简介:
除了一些免费的 API 接口,例如 http://ipinfo.io/223.155.166.172 可以得到一些信息外,还可以通过 python-geoip 库来解决这个问题。
shell > curl http://ipinfo.io/223.155.166.172{"ip":"223.155.166.172","hostname":"No Hostname","city":"Changsha","region":"Hunan","country":"CN","loc":"28.1792,113.1136","org":"AS4134 CHINANET-BACKBONE"}
示例:
1、安装 python-geoip、python-geoip-geolite2
shell > pipinstallpython-geoip-geolite2 -i https://pypi.douban.com/simple
# -i 使用豆瓣源
2、使用
shell >ipython
In [1]: from geoip import geolite2
In [2]: line = geolite2.lookup('223.155.166.172')
In [3]: line.get_info_dict
Out[3]: >In [4]: line.location
Out[4]: (28.1792,113.1136)