server.py 926 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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('/count')
  30. def get_count():
  31. """
  32. get the count of proxies
  33. :return: count, int
  34. """
  35. conn = get_conn()
  36. return str(conn.count())
  37. if __name__ == '__main__':
  38. app.run(host=API_HOST, port=API_PORT, threaded=API_THREADED)