server.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from flask import Flask, g
  2. from proxypool.storages.redis import RedisClient
  3. from proxypool.setting import API_HOST, API_PORT, API_THREADED, IS_DEV
  4. __all__ = ['app']
  5. app = Flask(__name__)
  6. if IS_DEV:
  7. app.debug = True
  8. def get_conn():
  9. """
  10. get redis client object
  11. :return:
  12. """
  13. if not hasattr(g, 'redis'):
  14. g.redis = RedisClient()
  15. return g.redis
  16. @app.route('/')
  17. def index():
  18. """
  19. get home page, you can define your own templates
  20. :return:
  21. """
  22. return '<h2>Welcome to Proxy Pool System</h2>'
  23. @app.route('/random')
  24. def get_proxy():
  25. """
  26. get a random proxy
  27. :return: get a random proxy
  28. """
  29. conn = get_conn()
  30. return conn.random().string()
  31. @app.route('/all')
  32. def get_proxy_all():
  33. """
  34. get a random proxy
  35. :return: get a random proxy
  36. """
  37. conn = get_conn()
  38. proxies = conn.all()
  39. proxies_string = ''
  40. if proxies:
  41. for proxy in proxies:
  42. proxies_string += str(proxy) + '\n'
  43. return proxies_string
  44. @app.route('/count')
  45. def get_count():
  46. """
  47. get the count of proxies
  48. :return: count, int
  49. """
  50. conn = get_conn()
  51. return str(conn.count())
  52. if __name__ == '__main__':
  53. app.run(host=API_HOST, port=API_PORT, threaded=API_THREADED)