非阻挡&多线程 - Socksipy&袜子

时间:2012-05-29 22:00:13

标签: python multithreading socks

我需要检查我的Proxies与特定服务器端口。我只是使用SocksiPy作为Socks的代理包装器。好吧它有效,但它该死的慢:/

我猜是因为我不使用非阻塞套接字。我试过.setblocking(0)但是我的所有支票都在瞬间“失败”。

#! /usr/bin/python
import time, Queue, threading, socks

t = time.time()
queue = Queue.Queue()

class ThreadUrl(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
    def run(self):
        while True:
            host = self.queue.get()
            id = host[0]
            ip = host[1]
            port = host[2]
            type = host[3]
            retry = host[4]

            try:
                s = socks.socksocket()
                s.settimeout(10)
                #s.setblocking(0) <- i tried this but well, when i use this all my proxy checks end up instant with FAIL :/
                if(type == 'HTTP' or type == 'HTTPS'):
                    s.setproxy(socks.PROXY_TYPE_HTTP, ip, int(port))
                else:
                    #because i dont know if the socks i have is for socks 4 or 5 i have to use this
                    if(retry == 1):
                        s.setproxy(socks.PROXY_TYPE_SOCKS5, ip, int(port))
                    else:
                        s.setproxy(socks.PROXY_TYPE_SOCKS4, ip, int(port))
                s.connect(('127.0.0.1', 1337))
                s.send('\r\n')
                data = s.recv(10)
                s.close()
                if( len(data) > 0 ):
                    print "GOOD PROXY: %s TYPE: %s" % (id, type)
            except:
                if(retry == 1):
                    print "SOCKS5 BAD: %s TYPE: %s" % (id, type)
                elif(retry == 0 and type == 'Socks 4/5'):
                    print "SOCKS4 BAD: %s TYPE: %s" % (id, type)
                    #because i dont know if the socks i have is for socks 4 or 5 i have to use this and requeue the proxy with the retry indicator set to 1 (socks5 testing)
                    queue.put([id, ip, port, type, 1])
                else:
                    print "HTTP/S BAD: %s TYPE: %s" % (id, type)

            self.queue.task_done()


def main():

    proxies = ((id,ip,port,type),(...))

    for i in range(4):
        t = ThreadUrl(queue)
        t.setDaemon(True)
        t.start()

    for item in proxies:
        queue.put([item['proxy_id'], item['proxy_ip'], item['proxy_port'], item['proxy_type'], 0])

    queue.join()

main()
print "Established Time: %.2f" % (time.time()-t)

感谢您的帮助,干杯:)

0 个答案:

没有答案