通过调用淘宝IP库获取IP归属地,脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib
import json
import sys
#淘宝ip库接口
url = "http://ip.taobao.com/service/getIpInfo.php?ip="
def ip_find(ip):
    data = urllib.urlopen(url + ip).read()
    datadict=json.loads(data)
    for oneinfo in datadict:
        if "code" == oneinfo:
            if datadict[oneinfo] == 0:
                return datadict["data"]["country"] + datadict["data"]["region"] + datadict["data"]["city"] + "\t" + datadict["data"]["isp"]
 
if __name__ ==  "__main__":
    ip=sys.argv[1]
    name=ip_find(ip)
    print sys.argv[1],name

使用方法:

1
2
$ python ip.py 202.106.0.20
202.106.0.20 中国北京市北京市   联通

 附:淘宝IP库接口说明

分类: Python