setting.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import platform
  2. from os.path import dirname, abspath, join
  3. from environs import Env
  4. from loguru import logger
  5. from proxypool.utils.parse import parse_redis_connection_string
  6. env = Env()
  7. env.read_env()
  8. # definition of flags
  9. IS_WINDOWS = platform.system().lower() == 'windows'
  10. # definition of dirs
  11. ROOT_DIR = dirname(dirname(abspath(__file__)))
  12. LOG_DIR = join(ROOT_DIR, env.str('LOG_DIR', 'logs'))
  13. # definition of environments
  14. DEV_MODE, TEST_MODE, PROD_MODE = 'dev', 'test', 'prod'
  15. APP_ENV = env.str('APP_ENV', DEV_MODE).lower()
  16. APP_DEBUG = env.bool('APP_DEBUG', True if APP_ENV == DEV_MODE else False)
  17. APP_DEV = IS_DEV = APP_ENV == DEV_MODE
  18. APP_PROD = IS_PROD = APP_DEV == PROD_MODE
  19. APP_TEST = IS_TEST = APP_ENV = TEST_MODE
  20. # redis host
  21. REDIS_HOST = env.str('REDIS_HOST', '127.0.0.1')
  22. # redis port
  23. REDIS_PORT = env.int('REDIS_PORT', 6379)
  24. # redis password, if no password, set it to None
  25. REDIS_PASSWORD = env.str('REDIS_PASSWORD', None)
  26. # redis connection string, like redis://[password]@host:port or rediss://[password]@host:port
  27. REDIS_CONNECTION_STRING = env.str('REDIS_CONNECTION_STRING', None)
  28. if REDIS_CONNECTION_STRING:
  29. REDIS_HOST, REDIS_PORT, REDIS_PASSWORD = parse_redis_connection_string(REDIS_CONNECTION_STRING)
  30. # redis hash table key name
  31. REDIS_KEY = env.str('REDIS_KEY', 'proxies:universal')
  32. # definition of proxy scores
  33. PROXY_SCORE_MAX = 100
  34. PROXY_SCORE_MIN = 0
  35. PROXY_SCORE_INIT = 10
  36. # definition of proxy number
  37. PROXY_NUMBER_MAX = 50000
  38. PROXY_NUMBER_MIN = 0
  39. # definition of tester cycle, it will test every CYCLE_TESTER second
  40. CYCLE_TESTER = env.int('CYCLE_TESTER', 20)
  41. # definition of getter cycle, it will get proxy every CYCLE_GETTER second
  42. CYCLE_GETTER = env.int('CYCLE_GETTER', 100)
  43. # definition of tester
  44. TEST_URL = env.str('TEST_URL', 'http://www.baidu.com')
  45. TEST_TIMEOUT = env.int('TEST_TIMEOUT', 10)
  46. TEST_BATCH = env.int('TEST_BATCH', 20)
  47. # TEST_HEADERS = env.json('TEST_HEADERS', {
  48. # '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',
  49. # })
  50. TEST_VALID_STATUS = env.list('TEST_VALID_STATUS', [200, 206, 302])
  51. # definition of api
  52. API_HOST = env.str('API_HOST', '0.0.0.0')
  53. API_PORT = env.int('API_PORT', 5555)
  54. API_THREADED = env.bool('API_THREADED', True)
  55. # flags of enable
  56. ENABLE_TESTER = env.bool('ENABLE_TESTER', True)
  57. ENABLE_GETTER = env.bool('ENABLE_GETTER', True)
  58. ENABLE_SERVER = env.bool('ENABLE_SERVER', True)
  59. logger.add(env.str('LOG_RUNTIME_FILE', 'runtime.log'), level='DEBUG', rotation='1 week', retention='20 days')
  60. logger.add(env.str('LOG_ERROR_FILE', 'error.log'), level='ERROR', rotation='1 week')