ProxyRefreshSchedule.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: ProxyRefreshSchedule.py
  5. Description : 代理定时刷新
  6. Author : JHao
  7. date: 2016/12/4
  8. -------------------------------------------------
  9. Change Activity:
  10. 2016/12/4: 代理定时刷新
  11. -------------------------------------------------
  12. """
  13. __author__ = 'JHao'
  14. from apscheduler.schedulers.blocking import BlockingScheduler
  15. from multiprocessing import Process
  16. import requests
  17. import time
  18. from Manager.ProxyManager import ProxyManager
  19. class ProxyRefreshSchedule(ProxyManager):
  20. """
  21. 代理定时刷新
  22. """
  23. def __init__(self):
  24. ProxyManager.__init__(self)
  25. def validProxy(self):
  26. self.db.changeTable(self.raw_proxy_queue)
  27. raw_proxy = self.db.pop()
  28. while raw_proxy:
  29. proxies = {"http": "http://{proxy}".format(proxy=raw_proxy),
  30. "https": "https://{proxy}".format(proxy=raw_proxy)}
  31. try:
  32. r = requests.get('https://www.baidu.com/', proxies=proxies, timeout=50, verify=False)
  33. if r.status_code == 200:
  34. self.db.changeTable(self.useful_proxy_queue)
  35. self.db.put(raw_proxy)
  36. except Exception as e:
  37. # print e
  38. pass
  39. self.db.changeTable(self.raw_proxy_queue)
  40. raw_proxy = self.db.pop()
  41. def refreshPool():
  42. pp = ProxyRefreshSchedule()
  43. pp.validProxy()
  44. def main(process_num=100):
  45. p = ProxyRefreshSchedule()
  46. p.refresh()
  47. for num in range(process_num):
  48. P = Process(target=refreshPool, args=())
  49. P.start()
  50. print '{time}: refresh complete!'.format(time=time.ctime())
  51. if __name__ == '__main__':
  52. # pp = ProxyRefreshSchedule()
  53. # pp.main()
  54. main()
  55. sched = BlockingScheduler()
  56. sched.add_job(main, 'interval', minute=20)
  57. sched.start()