CheckProxy.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. from getFreeProxy import GetFreeProxy
  15. from Util.utilFunction import verifyProxyFormat
  16. from Util.LogHandler import LogHandler
  17. log = LogHandler('check_proxy', file=False)
  18. class CheckProxy(object):
  19. @staticmethod
  20. def checkAllGetProxyFunc():
  21. """
  22. 检查getFreeProxy所有代理获取函数运行情况
  23. Returns:
  24. None
  25. """
  26. import inspect
  27. member_list = inspect.getmembers(GetFreeProxy, predicate=inspect.isfunction)
  28. proxy_count_dict = dict()
  29. for func_name, func in member_list:
  30. log.info(u"开始运行 {}".format(func_name))
  31. try:
  32. proxy_list = [_ for _ in func() if verifyProxyFormat(_)]
  33. proxy_count_dict[func_name] = len(proxy_list)
  34. except Exception as e:
  35. log.info(u"代理获取函数 {} 运行出错!".format(func_name))
  36. log.error(str(e))
  37. log.info(u"所有函数运行完毕 " + "***" * 5)
  38. for func_name, func in member_list:
  39. log.info(u"函数 {n}, 获取到代理数: {c}".format(n=func_name, c=proxy_count_dict.get(func_name, 0)))
  40. @staticmethod
  41. def checkGetProxyFunc(func):
  42. """
  43. 检查指定的getFreeProxy某个function运行情况
  44. Args:
  45. func: getFreeProxy中某个可调用方法
  46. Returns:
  47. None
  48. """
  49. func_name = getattr(func, '__name__', "None")
  50. log.info("start running func: {}".format(func_name))
  51. count = 0
  52. for proxy in func():
  53. if verifyProxyFormat(proxy):
  54. log.info("{} fetch proxy: {}".format(func_name, proxy))
  55. count += 1
  56. log.info("{n} completed, fetch proxy number: {c}".format(n=func_name, c=count))
  57. if __name__ == '__main__':
  58. CheckProxy.checkAllGetProxyFunc()
  59. CheckProxy.checkGetProxyFunc(GetFreeProxy.freeProxy01)