今天同事给了6W多个IP叫我查出ISP和归属地,果断用python urllib2搞之,数据库用是淘宝的API接口

#!/usr/bin/python#coding:utf-8import urllib2import jsonimport timeurl = 'http://ip.taobao.com/service/getIpInfo.php?ip='def checkTaobaoIP(ip):    try:        response = urllib2.urlopen(url + ip, timeout=5)        result = response.readlines()        data = json.loads(result[0])        return "%15s: %s-%s-%s" % (ip,data['data']['isp'],data['data']['region'],data['data']['city'])    except:        return "%15s: timeout" % ipif __name__ == "__main__":    f = open('ip.txt')    ips = f.readlines()    f.close()    f = open('ip-check.txt', 'w')    for ip in ips:        line = checkTaobaoIP(ip.strip())        if line:            print line.encode('utf-8')            f.write(line.encode('utf-8')+'\n')        else:            print line            f.write(line+'\n')    f.close()    print "Done!"