UsefulProxyCheck.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: UsefulProxyCheck
  5. Description : check useful proxy
  6. Author : JHao
  7. date: 2019/8/7
  8. -------------------------------------------------
  9. Change Activity:
  10. 2019/8/7: check useful proxy
  11. -------------------------------------------------
  12. """
  13. __author__ = 'JHao'
  14. from threading import Thread
  15. try:
  16. from Queue import Queue, Empty # py2
  17. except:
  18. from queue import Queue, Empty # py3
  19. from Util import LogHandler
  20. from Manager import ProxyManager
  21. from ProxyHelper import checkProxyUseful, Proxy
  22. FAIL_COUNT = 0
  23. class UsefulProxyCheck(ProxyManager, Thread):
  24. def __init__(self, queue, thread_name):
  25. ProxyManager.__init__(self)
  26. Thread.__init__(self, name=thread_name)
  27. self.queue = queue
  28. self.log = LogHandler('useful_proxy_check')
  29. def run(self):
  30. self.log.info("UsefulProxyCheck - {} : start".format(self.name))
  31. self.db.changeTable(self.useful_proxy_queue)
  32. while True:
  33. try:
  34. proxy_str = self.queue.get(block=False)
  35. except Empty:
  36. self.log.info("UsefulProxyCheck - {} : exit".format(self.name))
  37. break
  38. proxy_obj = Proxy.newProxyFromJson(proxy_str)
  39. proxy_obj, status = checkProxyUseful(proxy_obj)
  40. if status or proxy_obj.fail_count < FAIL_COUNT:
  41. if self.db.exists(proxy_obj.proxy):
  42. self.log.info('UsefulProxyCheck - {} : {} validation exists'.format(self.name,
  43. proxy_obj.proxy.ljust(20)))
  44. self.db.put(proxy_obj)
  45. self.log.info('UsefulProxyCheck - {} : {} validation pass'.format(self.name,
  46. proxy_obj.proxy.ljust(20)))
  47. else:
  48. self.log.info('UsefulProxyCheck - {} : {} validation fail'.format(self.name,
  49. proxy_obj.proxy.ljust(20)))
  50. self.db.delete(proxy_obj.proxy)
  51. self.queue.task_done()
  52. def doUsefulProxyCheck():
  53. proxy_queue = Queue()
  54. pm = ProxyManager()
  55. pm.db.changeTable(pm.useful_proxy_queue)
  56. for _proxy in pm.db.getAll():
  57. proxy_queue.put(_proxy)
  58. thread_list = list()
  59. for index in range(10):
  60. thread_list.append(UsefulProxyCheck(proxy_queue, "thread_%s" % index))
  61. for thread in thread_list:
  62. thread.start()
  63. for thread in thread_list:
  64. thread.join()
  65. if __name__ == '__main__':
  66. doUsefulProxyCheck()