setting.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: setting.py
  5. Description : 配置文件
  6. Author : JHao
  7. date: 2019/2/15
  8. -------------------------------------------------
  9. Change Activity:
  10. 2019/2/15:
  11. -------------------------------------------------
  12. """
  13. import sys
  14. from os import getenv
  15. from logging import getLogger
  16. log = getLogger(__name__)
  17. HEADER = """
  18. ****************************************************************
  19. *** ______ ********************* ______ *********** _ ********
  20. *** | ___ \_ ******************** | ___ \ ********* | | ********
  21. *** | |_/ / \__ __ __ _ __ _ | |_/ /___ * ___ | | ********
  22. *** | __/| _// _ \ \ \/ /| | | || __// _ \ / _ \ | | ********
  23. *** | | | | | (_) | > < \ |_| || | | (_) | (_) || |___ ****
  24. *** \_| |_| \___/ /_/\_\ \__ |\_| \___/ \___/ \_____/ ****
  25. **** __ / / *****
  26. ************************* /___ / *******************************
  27. ************************* ********************************
  28. ****************************************************************
  29. """
  30. PY3 = sys.version_info >= (3,)
  31. DB_TYPE = getenv('db_type', 'SSDB').upper()
  32. DB_HOST = getenv('db_host', '127.0.0.1')
  33. DB_PORT = getenv('db_port', '8080')
  34. DB_PASSWORD = getenv('db_password', '')
  35. """ 数据库配置 """
  36. DATABASES = {
  37. "default": {
  38. "TYPE": DB_TYPE,
  39. "HOST": DB_HOST,
  40. "PORT": DB_PORT,
  41. "NAME": "proxy",
  42. "PASSWORD": DB_PASSWORD
  43. }
  44. }
  45. # register the proxy getter function
  46. PROXY_GETTER = [
  47. "freeProxy01",
  48. "freeProxy02",
  49. "freeProxy03",
  50. "freeProxy04",
  51. "freeProxy05",
  52. "freeProxy06",
  53. "freeProxy07",
  54. "freeProxy08",
  55. "freeProxy09",
  56. ]
  57. """ API config http://127.0.0.1:5010 """
  58. SERVER_API = {
  59. "HOST": "0.0.0.0", # The ip specified which starting the web API
  60. "PORT": 5010 # port number to which the server listens to
  61. }
  62. class ConfigError(BaseException):
  63. pass
  64. def checkConfig():
  65. if DB_TYPE not in ["SSDB", "REDIS"]:
  66. raise ConfigError('db_type Do not support: %s, must SSDB/REDIS .' % DB_TYPE)
  67. if not DB_PORT.isdigit():
  68. raise ConfigError('db_port must be digit, not %s' % DB_PORT)
  69. from ProxyGetter import getFreeProxy
  70. illegal_getter = list(filter(lambda key: not hasattr(getFreeProxy.GetFreeProxy, key), PROXY_GETTER))
  71. if len(illegal_getter) > 0:
  72. raise ConfigError("ProxyGetter: %s does not exists" % "/".join(illegal_getter))
  73. checkConfig()