CheckProxy.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: CheckProxy
  5. Description : used for check getFreeProxy.py
  6. Author : JHao
  7. date: 2018/7/10
  8. -------------------------------------------------
  9. Change Activity:
  10. 2018/7/10: CheckProxy
  11. -------------------------------------------------
  12. """
  13. __author__ = 'JHao'
  14. import sys
  15. from getFreeProxy import GetFreeProxy
  16. from Util.utilFunction import verifyProxyFormat
  17. sys.path.append('../')
  18. from Util.LogHandler import LogHandler
  19. log = LogHandler('check_proxy', file=False)
  20. class CheckProxy(object):
  21. @staticmethod
  22. def checkAllGetProxyFunc():
  23. """
  24. 检查getFreeProxy所有代理获取函数运行情况
  25. Returns:
  26. None
  27. """
  28. import inspect
  29. member_list = inspect.getmembers(GetFreeProxy, predicate=inspect.isfunction)
  30. proxy_count_dict = dict()
  31. for func_name, func in member_list:
  32. log.info(u"开始运行 {}".format(func_name))
  33. try:
  34. proxy_list = [_ for _ in func() if verifyProxyFormat(_)]
  35. proxy_count_dict[func_name] = len(proxy_list)
  36. except Exception as e:
  37. log.info(u"代理获取函数 {} 运行出错!".format(func_name))
  38. log.error(str(e))
  39. log.info(u"所有函数运行完毕 " + "***" * 5)
  40. for func_name, func in member_list:
  41. log.info(u"函数 {n}, 获取到代理数: {c}".format(n=func_name, c=proxy_count_dict.get(func_name, 0)))
  42. @staticmethod
  43. def checkGetProxyFunc(func):
  44. """
  45. 检查指定的getFreeProxy某个function运行情况
  46. Args:
  47. func: getFreeProxy中某个可调用方法
  48. Returns:
  49. None
  50. """
  51. func_name = getattr(func, '__name__', "None")
  52. log.info("start running func: {}".format(func_name))
  53. count = 0
  54. for proxy in func():
  55. if verifyProxyFormat(proxy):
  56. log.info("{} fetch proxy: {}".format(func_name, proxy))
  57. count += 1
  58. log.info("{n} completed, fetch proxy number: {c}".format(n=func_name, c=count))
  59. if __name__ == '__main__':
  60. CheckProxy.checkAllGetProxyFunc()
  61. CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxyFirst)