Guest User

Untitled

a guest
Jan 13th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. #!/usr/bin/python
  2. import socket, threading, time, sys
  3. out = []
  4. lock = threading.Lock()
  5. total_thread = 100
  6. #Creat new out file
  7. open("out.txt", "w").close()
  8.  
  9. def worker(urls):
  10.     global out
  11.     print "Start thread"
  12.     for url in urls:
  13.         url = url.replace("http://", "").strip().lower()
  14.         s = socket.socket()
  15.         s.settimeout(5)
  16.         try:
  17.             s.connect((url, 80))
  18.         except:
  19.             print "Can't connect to "+url
  20.             continue
  21.         req = "GET / HTTP/1.1\r\n"
  22.         req += "Host: "+url+"\r\n"
  23.         req += "Content-type: text/html\r\n\r\n"
  24.         s.send(req)
  25.         try:
  26.             res = s.recv(20)
  27.         except:
  28.             print "Time out to read answer"
  29.             s.close()
  30.             continue
  31.         s.close()
  32.         if(res.find("200") != -1 or res.find("302") != -1 or res.find("301") != -1):
  33.             print url+" Oke"
  34.             out.append(url)
  35.  
  36. with open("urls.txt") as url:
  37.     qq = url.readlines()
  38.     arlen = len(qq)/total_thread
  39.     for i in xrange(total_thread):
  40.         tt = threading.Thread(target=worker, args=(qq[arlen*i:arlen*(i+1)],))
  41.         tt.start()
  42.     while True:
  43.         if(len(out) > 200):
  44.             lock.acquire()
  45.             with open("out.txt", "a") as ff:
  46.                 ff.write("\r\n".join(out))
  47.             out = []
  48.             print "LETS WRITE TO FILE!"
  49.             lock.release()
  50.         else:
  51.             time.sleep(5)
  52.         if(threading.activeCount() == 1):
  53.             with open("out.txt", "a") as ff:
  54.                 ff.write("\r\n".join(out))
  55.             print "End program"
  56.             sys.exit(0)
  57.         print "Thread alive "+str(threading.activeCount())
Advertisement
Add Comment
Please, Sign In to add comment