ProxyCheck.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. sys.path.append('../')
  17. from Util.utilFunction import validUsefulProxy
  18. from Manager.ProxyManager import ProxyManager
  19. from Util.LogHandler import LogHandler
  20. FAIL_COUNT = 1 # 校验失败次数, 超过次数删除代理
  21. class ProxyCheck(ProxyManager, Thread):
  22. def __init__(self, queue, item_dict):
  23. ProxyManager.__init__(self)
  24. Thread.__init__(self)
  25. self.log = LogHandler('proxy_check', file=False) # 多线程同时写一个日志文件会有问题
  26. self.queue = queue
  27. self.item_dict = item_dict
  28. def run(self):
  29. self.db.changeTable(self.useful_proxy_queue)
  30. while self.queue.qsize():
  31. proxy = self.queue.get()
  32. count = self.item_dict[proxy]
  33. if validUsefulProxy(proxy):
  34. # 验证通过计数器减1
  35. if count and int(count) > 0:
  36. self.db.put(proxy, num=int(count) - 1)
  37. else:
  38. pass
  39. self.log.info('ProxyCheck: {} validation pass'.format(proxy))
  40. else:
  41. self.log.info('ProxyCheck: {} validation fail'.format(proxy))
  42. if count and int(count) + 1 >= FAIL_COUNT:
  43. self.log.info('ProxyCheck: {} fail too many, delete!'.format(proxy))
  44. self.db.delete(proxy)
  45. else:
  46. self.db.put(proxy, num=int(count) + 1)
  47. self.queue.task_done()
  48. if __name__ == '__main__':
  49. # p = ProxyCheck()
  50. # p.run()
  51. pass