setting.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import platform
  2. from os.path import dirname, abspath, join
  3. from environs import Env
  4. from loguru import logger
  5. env = Env()
  6. env.read_env()
  7. # definition of flags
  8. IS_WINDOWS = platform.system().lower() == 'windows'
  9. # definition of dirs
  10. ROOT_DIR = dirname(dirname(abspath(__file__)))
  11. LOG_DIR = join(ROOT_DIR, env.str('LOG_DIR', 'logs'))
  12. # definition of environments
  13. DEV_MODE, TEST_MODE, PROD_MODE = 'dev', 'test', 'prod'
  14. APP_ENV = env.str('APP_ENV', DEV_MODE).lower()
  15. APP_DEBUG = env.bool('APP_DEBUG', True if APP_ENV == DEV_MODE else False)
  16. APP_DEV = IS_DEV = APP_ENV == DEV_MODE
  17. APP_PROD = IS_PROD = APP_ENV == PROD_MODE
  18. APP_TEST = IS_TEST = APP_ENV == TEST_MODE
  19. # Which WSGI container is used to run applications
  20. # - gevent: pip install gevent
  21. # - tornado: pip install tornado
  22. # - meinheld: pip install meinheld
  23. APP_PROD_METHOD_GEVENT = 'gevent'
  24. APP_PROD_METHOD_TORNADO = 'tornado'
  25. APP_PROD_METHOD_MEINHELD = 'meinheld'
  26. APP_PROD_METHOD = env.str('APP_PROD_METHOD', APP_PROD_METHOD_GEVENT).lower()
  27. # redis host
  28. REDIS_HOST = env.str('PROXYPOOL_REDIS_HOST',
  29. env.str('REDIS_HOST', '127.0.0.1'))
  30. # redis port
  31. REDIS_PORT = env.int('PROXYPOOL_REDIS_PORT', env.int('REDIS_PORT', 6379))
  32. # redis password, if no password, set it to None
  33. REDIS_PASSWORD = env.str('PROXYPOOL_REDIS_PASSWORD',
  34. env.str('REDIS_PASSWORD', None))
  35. # redis db, if no choice, set it to 0
  36. REDIS_DB = env.int('PROXYPOOL_REDIS_DB', env.int('REDIS_DB', 0))
  37. # redis connection string, like redis://[password]@host:port or rediss://[password]@host:port/0,
  38. # please refer to https://redis-py.readthedocs.io/en/stable/connections.html#redis.client.Redis.from_url
  39. REDIS_CONNECTION_STRING = env.str(
  40. 'PROXYPOOL_REDIS_CONNECTION_STRING', env.str('REDIS_CONNECTION_STRING', None))
  41. # redis hash table key name
  42. REDIS_KEY = env.str('PROXYPOOL_REDIS_KEY', env.str(
  43. 'REDIS_KEY', 'proxies:universal'))
  44. # definition of proxy scores
  45. PROXY_SCORE_MAX = 100
  46. PROXY_SCORE_MIN = 0
  47. PROXY_SCORE_INIT = 10
  48. # definition of proxy number
  49. PROXY_NUMBER_MAX = 50000
  50. PROXY_NUMBER_MIN = 0
  51. # definition of tester cycle, it will test every CYCLE_TESTER second
  52. CYCLE_TESTER = env.int('CYCLE_TESTER', 20)
  53. # definition of getter cycle, it will get proxy every CYCLE_GETTER second
  54. CYCLE_GETTER = env.int('CYCLE_GETTER', 100)
  55. GET_TIMEOUT = env.int('GET_TIMEOUT', 10)
  56. # definition of tester
  57. TEST_URL = env.str('TEST_URL', 'http://www.baidu.com')
  58. TEST_TIMEOUT = env.int('TEST_TIMEOUT', 10)
  59. TEST_BATCH = env.int('TEST_BATCH', 20)
  60. # only save anonymous proxy
  61. TEST_ANONYMOUS = env.bool('TEST_ANONYMOUS', True)
  62. # TEST_HEADERS = env.json('TEST_HEADERS', {
  63. # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
  64. # })
  65. TEST_VALID_STATUS = env.list('TEST_VALID_STATUS', [200, 206, 302])
  66. # definition of api
  67. API_HOST = env.str('API_HOST', '0.0.0.0')
  68. API_PORT = env.int('API_PORT', 5555)
  69. API_THREADED = env.bool('API_THREADED', True)
  70. # flags of enable
  71. ENABLE_TESTER = env.bool('ENABLE_TESTER', True)
  72. ENABLE_GETTER = env.bool('ENABLE_GETTER', True)
  73. ENABLE_SERVER = env.bool('ENABLE_SERVER', True)
  74. # logger.add(env.str('LOG_RUNTIME_FILE', join(LOG_DIR, 'runtime.log')), level='DEBUG', rotation='1 week', retention='20 days')
  75. # logger.add(env.str('LOG_ERROR_FILE', join(LOG_DIR, 'error.log')), level='ERROR', rotation='1 week')