扫代理的脚本

转载A大的,没试过



#!/usr/bin/python
import os
import subprocess
import re
import threading
import sys
import urllib2
import socket

a = 222
b = 73
c = 1
d = 1
port = 8080
THREAD = 10000

CHECK_URL = 'http://xxx.com/list_tool.ini'
CHECK_KEY = 'xxx'
PROXY_LOG = '/root/proxy_scan.txt'

lock = threading.Lock()

def proxy_check(proxy):
        proxy_url = 'http://'+proxy
        proxy_support = urllib2.ProxyHandler({'http': proxy_url})
        opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
        r=urllib2.Request(CHECK_URL)
        r.add_header("Accept-Language","zh-cn")
        r.add_header("Content-Type","text/html; charset=gb2312")
        r.add_header("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)")
        trycount=1
        while trycount<=1:
                try:
                        f=opener.open(r)
                        data=f.read()
                        if CHECK_KEY in data:
                                # write to log
                                os.system("echo %s >> %s" % (proxy, PROXY_LOG))
                                return
                except Exception, e:
                        #print e
                        trycount=trycount+1
        if trycount>2:
                return Falses


def getOneIp():
    global a, b, c, d
    if d == 255:
        c += 1
        d = 1
    if c == 255:
        b += 1
        c = 1
    if b == 255:
        return False
    
    d += 1
    return "%d.%d.%d.%d" % (a, b, c, d)


def scan():
    global port

    if lock.acquire():
        ip = getOneIp()
        lock.release()
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(0.6)
    try:
        s.connect((ip, port))
        print '%s:%d open' % (ip, port)
        proxy_check(ip + ':' + str(port))
        s.close()
    except Exception, e:
        print '%s:%d close %s' % (ip, port, e)
        pass
        

while True:

    threads = []
    for x in xrange(0, THREAD):
        threads.append(threading.Thread(target=scan))
    for t in threads:
        t.start()
    for t in threads:
        t.join()

发表评论