Python多线程批量验证HTTP代理


环境要求 :


  1. Python 2.7
  2. Requests库 pip install requests

验证脚本 :


#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import threading
import requests

# config-start
testUrl = "http://1212.ip138.com/ic.asp" # 利用了ip138的IP查询接口
timeout = 5 # 设置超时
threadNumber = 50 # 设置线程数
proxiesFileName = "proxies.txt"
successFileName = "success.txt"
# config-end

def testOnline(ip,port):
    '''
    测试HTTP代理是否可用
        利用IP138的接口 , 在响应的页面中寻找本机IP , 如果找到 , 则说明代理可以成功连接
    '''
    global successFileName
    global testUrl
    global timeout
    keyWord = ip
    proxies = {"http":"http://"+ip+":"+port}
    try:
        content=requests.get(testUrl,proxies=proxies,timeout=timeout).text
        if keyWord in content:
            print ip+":"+port
            file=open(successFileName,"a+")
            file.write(ip+":"+port+"\n")
            file.close()
        else:
            print "Proxy Error..."
    except Exception as e:
        print "NetWork Error..."

class myThread (threading.Thread):
    def __init__(self, ip, port):
        threading.Thread.__init__(self)
        self.ip = ip
        self.port = port
    def run(self):
        testOnline(self.ip,self.port)

proxies=open(proxiesFileName,"r")

threads = [] # 线程池

for proxy in proxies:
    line = proxy[0:-1]
    ip = line.split(":")[0] # 获取IP
    port = line.split(":")[1]
    threads.append(myThread(ip,port))

for t in threads:
    t.start()
    while True:
        if(len(threading.enumerate())

测试代理IP数据 :


60.31.185.90:8080
218.108.89.245:808
222.82.222.242:9999
60.206.222.157:3128
60.207.180.1:3128
61.168.162.3:80
218.76.106.78:3128
223.68.1.38:8000
1.82.216.134:80
220.160.22.115:80
122.72.18.160:80
58.217.195.141:80
223.67.136.218:80
60.206.113.252:3128
60.206.60.153:3128
122.72.33.138:80
124.133.230.254:80
1.82.216.135:80
114.215.101.42:3128
202.107.238.51:3128
114.215.150.13:3128
60.207.239.247:3128
203.70.11.186:80
211.153.17.151:80
60.207.189.250:3128
60.207.239.245:3128
111.204.0.194:9999
60.207.239.246:3128
218.56.132.157:8080
183.63.110.202:3128
123.65.217.151:9797
203.91.121.74:3128
218.67.126.15:3128
118.144.213.85:3128
115.231.219.202:3128
60.206.183.249:3128
113.200.159.155:9999
218.56.132.154:8080
202.105.179.164:3128
60.207.135.221:3128
162.105.80.111:3128
124.206.111.158:3128
60.207.175.2:3128
123.57.180.234:3128
118.144.154.253:3128
122.224.227.202:3128
218.56.132.158:8080
218.56.132.156:8080
60.206.148.135:3128
60.206.229.152:3128
221.204.11.8:8080
115.238.228.9:8080
60.191.160.20:3128
113.102.153.123:9999
61.163.39.70:9999
60.191.164.226:3128
123.57.58.164:80
124.88.67.54:80
122.226.255.254:3128
123.139.59.85:9999
58.242.248.5:80
60.191.164.83:3128
119.10.72.33:8088
122.224.215.14:3128
14.119.46.231:9999
218.75.149.207:3128
112.95.56.37:9999
27.46.48.137:8888
221.219.230.62:9000
119.129.96.39:9797
119.29.103.13:8888
123.5.57.136:9999
61.52.129.79:9999
123.15.44.40:9999
14.119.40.131:8080

结果 :


Python多线程批量验证HTTP代理_第1张图片
Paste_Image.png
122.72.33.138:80
58.217.195.141:80
1.82.216.135:80
220.160.22.115:80
1.82.216.134:80
122.72.18.160:80
114.215.101.42:3128
114.215.150.13:3128
123.65.217.151:9797
58.242.248.5:80
61.168.162.3:80
122.224.227.202:3128
223.68.1.38:8000
124.88.67.54:80
202.107.238.51:3128
218.56.132.154:8080
183.63.110.202:3128
203.91.121.74:3128
162.105.80.111:3128
115.231.219.202:3128
123.5.57.136:9999
60.191.164.83:3128
218.67.126.15:3128
122.72.33.138:80
1.82.216.134:80
58.217.195.141:80
1.82.216.135:80
122.72.18.160:80
220.160.22.115:80
122.224.227.202:3128
58.242.248.5:80
221.204.11.8:8080
223.68.1.38:8000
124.88.67.54:80
114.215.150.13:3128
114.215.101.42:3128
122.72.33.138:80
58.217.195.141:80
1.82.216.135:80
1.82.216.134:80
220.160.22.115:80
122.72.18.160:80
114.215.150.13:3128
58.242.248.5:80
61.168.162.3:80
223.68.1.38:8000
122.224.227.202:3128
119.29.103.13:8888
202.107.238.51:3128
123.5.57.136:9999
114.215.101.42:3128
123.57.180.234:3128
202.105.179.164:3128

你可能感兴趣的:(Python多线程批量验证HTTP代理)