ProxyCheck.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: ProxyCheck
  5. Description : 多线程验证useful_proxy
  6. Author : J_hao
  7. date: 2017/9/26
  8. -------------------------------------------------
  9. Change Activity:
  10. 2017/9/26: 多线程验证useful_proxy
  11. -------------------------------------------------
  12. """
  13. __author__ = 'J_hao'
  14. import sys
  15. from threading import Thread
  16. try:
  17. from Queue import Empty # py3
  18. except:
  19. from queue import Empty # py2
  20. sys.path.append('../')
  21. from Util.utilFunction import validUsefulProxy
  22. from Manager.ProxyManager import ProxyManager
  23. from Util.LogHandler import LogHandler
  24. FAIL_COUNT = 1 # 校验失败次数, 超过次数删除代理
  25. class ProxyCheck(ProxyManager, Thread):
  26. def __init__(self, queue, item_dict):
  27. ProxyManager.__init__(self)
  28. Thread.__init__(self)
  29. self.log = LogHandler('proxy_check', file=False) # 多线程同时写一个日志文件会有问题
  30. self.queue = queue
  31. self.item_dict = item_dict
  32. def run(self):
  33. self.db.changeTable(self.useful_proxy_queue)
  34. while True:
  35. try:
  36. proxy = self.queue.get(block=False)
  37. except Empty:
  38. break
  39. count = self.item_dict[proxy]
  40. if validUsefulProxy(proxy):
  41. # 验证通过计数器减1
  42. if count and int(count) > 0:
  43. self.db.put(proxy, num=int(count) - 1)
  44. else:
  45. pass
  46. self.log.info('ProxyCheck: {} validation pass'.format(proxy))
  47. else:
  48. self.log.info('ProxyCheck: {} validation fail'.format(proxy))
  49. if count and int(count) + 1 >= FAIL_COUNT:
  50. self.log.info('ProxyCheck: {} fail too many, delete!'.format(proxy))
  51. self.db.delete(proxy)
  52. else:
  53. self.db.put(proxy, num=int(count) + 1)
  54. self.queue.task_done()