首页 > 代码库 > 批量查询ip地址归属地

批量查询ip地址归属地

 1 #!/usr/bin/env python 2 #-*-coding:utf-8-*- 3 import urllib 4 import urllib2 5 import json 6 import time 7 import sys 8 def check_ipaddr(func,*args, **kwargs): 9     iplist = func(*args,**kwargs)10     temp_list =[]11     for line in iplist:12         if line.split():13             a = json.loads(line)14             b = []15             for item in a:16                 if item not in b:17                     b.append(item)18             b = "".join(b)19             temp_list.append(b)20     return temp_list21 22 def check_url(res):23     ip_list = []24     f = open(res,r)25     for line in f:26         url = "http://freeapi.ipip.net/%s" %line.strip()27         ip_addr = urllib.urlopen(url).read()28         time.sleep(0.25)29         ip_list.append(ip_addr)30     f.flush()31     #print len(ip_list)32     return ip_list33 34 def ip_city(ip_file):35     ip_list = []36     city_list = []37     temp =check_ipaddr(check_url,ip_file)38     f = open(ip_file,r)39     for i in f:40         line = i.strip().split(\n)41         for i in line:42             ip_list.append(i)43             #f.flush()44     for line in temp:45         city_list.append(line)46     for i in range(len(ip_list)):47         l =i+148         time.sleep(0.07)49         print "%s)"%l,ip_list[i],"\t"+"("+ city_list[i] +")"50     51     f.flush52     return ip_city53 54 if __name__ == "__main__":55     if len(sys.argv) >1:56         ip_city(sys.argv[1])57     else:58         print "\033[31;5mPlease pass a file....\033[0m"

 

批量查询ip地址归属地