接上一篇保存的IP地址,进行验证
# -*- coding: utf-8 -*-
import requests
from threading import Thread
import threading
get_ip = open('get_ip.txt','r')
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0'}
lock = threading.Lock() #使用线程锁
#利用多线程,在新浪IP接口上,验证IP
def verify_ip():
url = 'http://www.baidu.com' # 新浪IP接口
try:
# 先要获取锁:
lock.acquire()
ip = get_ip.readline().strip()
#解开锁
lock.release()
proxies = {'http': ip}
r = requests.get(url, headers=headers, proxies=proxies,timeout=5)
#如果requests成功,表示验证成功,打印出IP
print ip
except:
pass
thread_all = []
for i in range(100): #根据情况决定
t = Thread(target=verify_ip)
thread_all.append(t)
t.start()
for t in thread_all:
t.join()
get_ip.close()