Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import socket, threading, time, sys
- out = []
- lock = threading.Lock()
- total_thread = 100
- #Creat new out file
- open("out.txt", "w").close()
- def worker(urls):
- global out
- print "Start thread"
- for url in urls:
- url = url.replace("http://", "").strip().lower()
- s = socket.socket()
- s.settimeout(5)
- try:
- s.connect((url, 80))
- except:
- print "Can't connect to "+url
- continue
- req = "GET / HTTP/1.1\r\n"
- req += "Host: "+url+"\r\n"
- req += "Content-type: text/html\r\n\r\n"
- s.send(req)
- try:
- res = s.recv(20)
- except:
- print "Time out to read answer"
- s.close()
- continue
- s.close()
- if(res.find("200") != -1 or res.find("302") != -1 or res.find("301") != -1):
- print url+" Oke"
- out.append(url)
- with open("urls.txt") as url:
- qq = url.readlines()
- arlen = len(qq)/total_thread
- for i in xrange(total_thread):
- tt = threading.Thread(target=worker, args=(qq[arlen*i:arlen*(i+1)],))
- tt.start()
- while True:
- if(len(out) > 200):
- lock.acquire()
- with open("out.txt", "a") as ff:
- ff.write("\r\n".join(out))
- out = []
- print "LETS WRITE TO FILE!"
- lock.release()
- else:
- time.sleep(5)
- if(threading.activeCount() == 1):
- with open("out.txt", "a") as ff:
- ff.write("\r\n".join(out))
- print "End program"
- sys.exit(0)
- print "Thread alive "+str(threading.activeCount())
Advertisement
Add Comment
Please, Sign In to add comment