Advertisement
Guest User

check.py

a guest
Nov 11th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. # Network
  2. import urllib.request, urllib.parse, urllib.error
  3. import http.cookiejar
  4.  
  5. # Concurrency
  6. import threading
  7. import queue
  8. import itertools
  9.  
  10. import pycurl
  11.  
  12. # Etc
  13. import time
  14. from colorama import Fore, Back, Style
  15.  
  16. # Global variables
  17. #in_filename = 'input/3.txt'
  18. in_directory = './input/'
  19. out_filename = 'output/out_filtered.txt'
  20. test_url = 'http://www.google.com/humans.txt'
  21. thread_number = 500
  22. timeout_value = 1
  23.  
  24. good_message = Fore.GREEN + "GOOD! " + Fore.RESET
  25. bad_message = Fore.RED + "BAD " + Fore.RESET
  26.  
  27. # Stats
  28. good_proxy_num = itertools.count()
  29. start_time = time.time()
  30. end_time = time.time()
  31.  
  32. # Safe print()
  33. mylock = threading.Lock()
  34. def sprint(*a, **b):
  35. with mylock:
  36. print(*a, **b)
  37.  
  38.  
  39. #
  40. # Printer
  41. #
  42. class PrintThread(threading.Thread):
  43. def __init__(self, queue, filename):
  44. threading.Thread.__init__(self)
  45. self.queue = queue
  46. self.output = open(filename, 'a')
  47. self.shutdown = False
  48.  
  49. def write(self, line):
  50. print(line, file=self.output)
  51.  
  52. def run(self):
  53. while not self.shutdown:
  54. lines = self.queue.get()
  55. self.write(lines)
  56. self.queue.task_done()
  57.  
  58. def terminate(self):
  59. self.output.close()
  60. self.shutdown = True
  61.  
  62.  
  63.  
  64. #
  65. # Processor
  66. #
  67. class ProcessThread(threading.Thread):
  68. def __init__(self, id, task_queue, out_queue):
  69. threading.Thread.__init__(self)
  70. self.task_queue = task_queue
  71. self.out_queue = out_queue
  72. self.id = id
  73.  
  74. # ...
  75. def run(self):
  76. while True:
  77. task = self.task_queue.get()
  78. result = self.process(task)
  79.  
  80. if result is not None:
  81. self.out_queue.put(result)
  82. next(good_proxy_num)
  83.  
  84. self.task_queue.task_done()
  85.  
  86.  
  87. # Do the processing job here
  88. def process(self, task):
  89. proxy = task
  90. log_msg = str("Thread #%3d. Trying HTTP proxy %21s \t\t" % (self.id, proxy))
  91. c1 = pycurl.Curl()
  92. c1.setopt(pycurl.URL, test_url)
  93. c1.setopt(pycurl.PROXY, proxy.split(':')[0])
  94. c1.setopt(pycurl.PROXYPORT, int(proxy.split(':')[1]))
  95. c1.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)
  96. c1.setopt(pycurl.CONNECTTIMEOUT, self.timeout_value)
  97. try:
  98. t1 = time.time()
  99. response = c1.perform()
  100. t2 = time.time()
  101. except Exception as e:
  102. log_msg += "%s (%s)" % (bad_message, str(e))
  103. sprint(log_msg)
  104. return None
  105.  
  106. log_msg += good_message + " Response time: %d, response code=%s" % ( int((t2-t1)*1000), c1.getinfo(pycurl.HTTP_CODE) )
  107. sprint(log_msg)
  108. return proxy
  109.  
  110. def terminate(self):
  111. None
  112. #print("Thread #%d is down..." % (self.id))
  113.  
  114. #
  115. # Main starts here
  116. #
  117. # Init some stuff
  118. input_queue = queue.Queue()
  119. result_queue = queue.Queue()
  120.  
  121.  
  122. # Spawn worker threads
  123. workers = []
  124. for i in range(0, thread_number):
  125. t = ProcessThread(i, input_queue, result_queue)
  126. t.setDaemon(True)
  127. t.start()
  128. workers.append(t)
  129.  
  130. # Spawn printer thread to print
  131. f_printer = PrintThread(result_queue, out_filename)
  132. f_printer.setDaemon(True)
  133. f_printer.start()
  134.  
  135. # Add some stuff to the input queue
  136. start_time = time.time()
  137.  
  138. proxy_list = []
  139. import os
  140. for root, dirs, files in os.walk(in_directory):
  141. for file in files:
  142. if file.endswith(".txt"):
  143. # read all lines from file
  144. file_line_list = [line.rstrip('\n') for line in open(os.path.join(root, file), 'r')]
  145. # append to proxy_list
  146. proxy_list.extend(file_line_list)
  147.  
  148. for proxy in proxy_list:
  149. input_queue.put(proxy)
  150.  
  151. total_proxy_num = len(proxy_list)
  152. print("got %d proxies to check" % total_proxy_num)
  153.  
  154. if total_proxy_num == 0:
  155. exit()
  156.  
  157. # Wait for queue to get empty
  158. input_queue.join()
  159. result_queue.join()
  160.  
  161.  
  162. #while (not input_queue.empty()):
  163. # time.sleep(1)
  164.  
  165.  
  166. # Shutdown
  167. f_printer.terminate()
  168.  
  169. for worker in workers:
  170. worker.terminate()
  171.  
  172. # Print some info
  173. good_proxy_num = float(next(good_proxy_num))
  174. print("In: %d. Good: %d, that's %.2f%%" % (total_proxy_num, good_proxy_num, 100.0 * good_proxy_num/total_proxy_num))
  175.  
  176. end_time = time.time()
  177. print("Time elapsed: %.1f seconds." % (end_time - start_time))
  178. print("Bye-bye!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement