setting.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 = env.str('APP_PROD_METHOD', "gevent").lower()
  24. # redis host
  25. REDIS_HOST = env.str('PROXYPOOL_REDIS_HOST',
  26. env.str('REDIS_HOST', '127.0.0.1'))
  27. # redis port
  28. REDIS_PORT = env.int('PROXYPOOL_REDIS_PORT', env.int('REDIS_PORT', 6379))
  29. # redis password, if no password, set it to None
  30. REDIS_PASSWORD = env.str('PROXYPOOL_REDIS_PASSWORD',
  31. env.str('REDIS_PASSWORD', None))
  32. # redis db, if no choice, set it to 0
  33. REDIS_DB = env.int('PROXYPOOL_REDIS_DB', env.int('REDIS_DB', 0))
  34. # redis connection string, like redis://[password]@host:port or rediss://[password]@host:port/0,
  35. # please refer to https://redis-py.readthedocs.io/en/stable/connections.html#redis.client.Redis.from_url
  36. REDIS_CONNECTION_STRING = env.str(
  37. 'PROXYPOOL_REDIS_CONNECTION_STRING', env.str('REDIS_CONNECTION_STRING', None))
  38. # redis hash table key name
  39. REDIS_KEY = env.str('PROXYPOOL_REDIS_KEY', env.str(
  40. 'REDIS_KEY', 'proxies:universal'))
  41. # definition of proxy scores
  42. PROXY_SCORE_MAX = 100
  43. PROXY_SCORE_MIN = 0
  44. PROXY_SCORE_INIT = 10
  45. # definition of proxy number
  46. PROXY_NUMBER_MAX = 50000
  47. PROXY_NUMBER_MIN = 0
  48. # definition of tester cycle, it will test every CYCLE_TESTER second
  49. CYCLE_TESTER = env.int('CYCLE_TESTER', 20)
  50. # definition of getter cycle, it will get proxy every CYCLE_GETTER second
  51. CYCLE_GETTER = env.int('CYCLE_GETTER', 100)
  52. GET_TIMEOUT = env.int('GET_TIMEOUT', 10)
  53. # definition of tester
  54. TEST_URL = env.str('TEST_URL', 'http://www.baidu.com')
  55. TEST_TIMEOUT = env.int('TEST_TIMEOUT', 10)
  56. TEST_BATCH = env.int('TEST_BATCH', 20)
  57. # only save anonymous proxy
  58. TEST_ANONYMOUS = True
  59. # TEST_HEADERS = env.json('TEST_HEADERS', {
  60. # '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',
  61. # })
  62. TEST_VALID_STATUS = env.list('TEST_VALID_STATUS', [200, 206, 302])
  63. # definition of api
  64. API_HOST = env.str('API_HOST', '0.0.0.0')
  65. API_PORT = env.int('API_PORT', 5555)
  66. API_THREADED = env.bool('API_THREADED', True)
  67. # flags of enable
  68. ENABLE_TESTER = env.bool('ENABLE_TESTER', True)
  69. ENABLE_GETTER = env.bool('ENABLE_GETTER', True)
  70. ENABLE_SERVER = env.bool('ENABLE_SERVER', True)
  71. # logger.add(env.str('LOG_RUNTIME_FILE', join(LOG_DIR, 'runtime.log')), level='DEBUG', rotation='1 week', retention='20 days')
  72. # logger.add(env.str('LOG_ERROR_FILE', join(LOG_DIR, 'error.log')), level='ERROR', rotation='1 week')