一. IP 地址归属地批量查询任务
使用创建子类的方式实现多线程任务
from threading import Thread,Lock
import requests
import json
class GetHost(Thread):
def __init__(self, ip):
super(GetHost, self).__init__()
self.ip = ip
def run(self):
url = 'http://ip-api.com/json/%s' % self.ip
page = requests.get(url).text
dict_data = json.loads(page)
lock.acquire()
city = dict_data.get('city')
country = dict_data.get('country')
# print(self.ip,city,country)
print("IP:%s City:%s Country:%s"%(self.ip, city, country))
lock.release()
if __name__ == '__main__':
threads = []
lock=Lock()
for i in range(1, 10):
ip ='1.1.1.' + str(i)
t = GetHost(ip)
t.start()
threads.append(t)
[thread.join() for thread in threads]