Non-blocking and multithreaded processing - Socksipy & Socks

I need to check my proxies against a specific server port. I just use SocksiPy as a proxy for Socks. Well, it works, but its damn slow: /

I guess because I do not use non-blocking sockets. I tried .setblocking (0), but then all my checks are “FAIL” in an instant.

#! /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) 

Thanks for your help, Cheers :)

+4
source share

Source: https://habr.com/ru/post/1415002/


All Articles