server.py 2.7 KB

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