ProxyValidSchedule.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: ProxyValidSchedule.py
  5. Description : 验证useful_proxy_queue中的代理,将不可用的移出
  6. Author : JHao
  7. date: 2017/3/31
  8. -------------------------------------------------
  9. Change Activity:
  10. 2017/3/31: 验证useful_proxy_queue中的代理
  11. -------------------------------------------------
  12. """
  13. __author__ = 'JHao'
  14. import sys
  15. sys.path.append('../')
  16. from Schedule.ProxyCheck import ProxyCheck
  17. from Manager.ProxyManager import ProxyManager
  18. from queue import Queue
  19. import time
  20. class ProxyValidSchedule(ProxyManager, object):
  21. def __init__(self):
  22. ProxyManager.__init__(self)
  23. self.queue = Queue()
  24. def __validProxy(self, threads=10):
  25. """
  26. 验证useful_proxy代理
  27. :param threads: 线程数
  28. :return:
  29. """
  30. thread_list = list()
  31. for index in range(threads):
  32. thread_list.append(ProxyCheck(self.queue, self.item_dict))
  33. for thread in thread_list:
  34. thread.daemon = True
  35. thread.start()
  36. for thread in thread_list:
  37. thread.join()
  38. def main(self):
  39. self.put_queue()
  40. while True:
  41. if self.queue.qsize():
  42. self.__validProxy()
  43. else:
  44. print('Time sleep 5 minutes.')
  45. time.sleep(60 * 1)
  46. self.put_queue()
  47. def put_queue(self):
  48. self.db.changeTable(self.useful_proxy_queue)
  49. self.item_dict = self.db.getAll()
  50. for item in self.item_dict:
  51. self.queue.put(item)
  52. def run():
  53. p = ProxyValidSchedule()
  54. p.main()
  55. if __name__ == '__main__':
  56. p = ProxyValidSchedule()
  57. p.main()