api.py 581 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from flask import Flask, g
  2. from .db import RedisClient
  3. __all__ = ['app']
  4. app = Flask(__name__)
  5. def get_conn():
  6. if not hasattr(g, 'redis'):
  7. g.redis = RedisClient()
  8. return g.redis
  9. @app.route('/')
  10. def index():
  11. return '<h2>Welcome to Proxy Pool System</h2>'
  12. @app.route('/random')
  13. def get_proxy():
  14. """
  15. Get a proxy
  16. """
  17. conn = get_conn()
  18. return conn.random()
  19. @app.route('/count')
  20. def get_counts():
  21. """
  22. Get the count of proxies
  23. """
  24. conn = get_conn()
  25. return str(conn.count())
  26. if __name__ == '__main__':
  27. app.run()