ProxyCheck.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 time import sleep
  16. from threading import Thread
  17. sys.path.append('../')
  18. from Util.utilFunction import validUsefulProxy
  19. from Manager.ProxyManager import ProxyManager
  20. from Util.LogHandler import LogHandler
  21. FAIL_COUNT = 2 # 校验失败次数, 超过次数删除代理
  22. class ProxyCheck(ProxyManager, Thread):
  23. def __init__(self):
  24. ProxyManager.__init__(self)
  25. Thread.__init__(self)
  26. self.log = LogHandler('proxy_check')
  27. def run(self):
  28. self.db.changeTable(self.useful_proxy_queue)
  29. while True:
  30. for proxy, count in self.db.getAll().items():
  31. if validUsefulProxy(proxy):
  32. # 验证通过计数器减1
  33. if count and int(count) > 0:
  34. self.db.put(proxy, num=int(count) - 1)
  35. else:
  36. pass
  37. self.log.info('ProxyCheck: {} validation pass'.format(proxy))
  38. else:
  39. self.log.info('ProxyCheck: {} validation fail'.format(proxy))
  40. if count and int(count) > FAIL_COUNT:
  41. self.log.info('ProxyCheck: {} fail too many, delete!'.format(proxy))
  42. self.db.delete(proxy)
  43. else:
  44. self.db.put(proxy, num=int(count) + 1)
  45. sleep(60 * 5)
  46. if __name__ == '__main__':
  47. p = ProxyCheck()
  48. p.run()