Python多線程批量驗證HTTP代理


環(huán)境要求 :


  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 # 設置線程數(shù)
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())<threadNumber):
            break

測試代理IP數(shù)據(jù) :


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

結果 :


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
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 環(huán)境管理管理Python版本和環(huán)境的工具。p–非常簡單的交互式python版本管理工具。pyenv–簡單的Pyth...
    MrHamster閱讀 3,957評論 1 61
  • # Python 資源大全中文版 我想很多程序員應該記得 GitHub 上有一個 Awesome - XXX 系列...
    aimaile閱讀 26,840評論 6 427
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,628評論 19 139
  • GitHub 上有一個 Awesome - XXX 系列的資源整理,資源非常豐富,涉及面非常廣。awesome-p...
    若與閱讀 19,334評論 4 417
  • 22年12月更新:個人網(wǎng)站關停,如果仍舊對舊教程有興趣參考 Github 的markdown內(nèi)容[https://...
    tangyefei閱讀 35,421評論 22 257

友情鏈接更多精彩內(nèi)容