server.py 1.2 KB

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