server.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import hmac
  2. from flask import Flask, g, request
  3. from proxypool.exceptions import PoolEmptyException
  4. from proxypool.storages.redis import RedisClient
  5. from proxypool.setting import API_HOST, API_PORT, API_THREADED, API_KEY, IS_DEV, PROXY_RAND_KEY_DEGRADED
  6. import functools
  7. __all__ = ['app']
  8. app = Flask(__name__)
  9. if IS_DEV:
  10. app.debug = True
  11. def auth_required(func):
  12. @functools.wraps(func)
  13. def decorator(*args, **kwargs):
  14. # conditional decorator, when setting API_KEY is set, otherwise just ignore this decorator
  15. if API_KEY == "":
  16. return func(*args, **kwargs)
  17. if request.headers.get('API-KEY', None) is not None:
  18. api_key = request.headers.get('API-KEY')
  19. else:
  20. return {"message": "Please provide an API key in header"}, 400
  21. # Check if API key is correct and valid
  22. if request.method == "GET" and hmac.compare_digest(api_key, API_KEY):
  23. return func(*args, **kwargs)
  24. else:
  25. return {"message": "The provided API key is not valid"}, 403
  26. return decorator
  27. def get_conn():
  28. """
  29. get redis client object
  30. :return:
  31. """
  32. if not hasattr(g, 'redis'):
  33. g.redis = RedisClient()
  34. return g.redis
  35. @app.route('/')
  36. @auth_required
  37. def index():
  38. """
  39. get home page, you can define your own templates
  40. :return:
  41. """
  42. return '<h2>Welcome to Proxy Pool System</h2>'
  43. @app.route('/random')
  44. @auth_required
  45. def get_proxy():
  46. """
  47. get a random proxy, can query the specific sub-pool according the (redis) key
  48. if PROXY_RAND_KEY_DEGRADED is set to True, will get a universal random proxy if no proxy found in the sub-pool
  49. :return: get a random proxy
  50. """
  51. key = request.args.get('key')
  52. conn = get_conn()
  53. # return conn.random(key).string() if key else conn.random().string()
  54. if key:
  55. try:
  56. return conn.random(key).string()
  57. except PoolEmptyException:
  58. if not PROXY_RAND_KEY_DEGRADED:
  59. raise
  60. return conn.random().string()
  61. @app.route('/all')
  62. @auth_required
  63. def get_proxy_all():
  64. """
  65. get a random proxy
  66. :return: get a random proxy
  67. """
  68. key = request.args.get('key')
  69. conn = get_conn()
  70. proxies = conn.all(key) if key else conn.all()
  71. proxies_string = ''
  72. if proxies:
  73. for proxy in proxies:
  74. proxies_string += str(proxy) + '\n'
  75. return proxies_string
  76. @app.route('/count')
  77. @auth_required
  78. def get_count():
  79. """
  80. get the count of proxies
  81. :return: count, int
  82. """
  83. conn = get_conn()
  84. key = request.args.get('key')
  85. return str(conn.count(key)) if key else conn.count()
  86. if __name__ == '__main__':
  87. app.run(host=API_HOST, port=API_PORT, threaded=API_THREADED)