简介:
除了一些免费的 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 > pip install python-geoip-geolite2 -i https://pypi.douban.com/simple
# -i 使用豆瓣源
2、使用
shell > ipython In []: from geoip import geolite2 In []: line = geolite2.lookup('223.155.166.172') In []: line.get_info_dict
Out[]: <bound method IPInfo.get_info_dict of <IPInfo ip='223.155.166.172' country='CN' continent='AS' subdivisions=frozenset(['']) timezone='Asia/Shanghai' location=(28.1792, 113.1136)>> In []: line.location
Out[]: (28.1792, 113.1136)