api.py 672 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. """Opens a new redis connection if there is none yet for the
  7. current application context.
  8. """
  9. if not hasattr(g, 'redis_client'):
  10. g.redis_client = RedisClient()
  11. return g.redis_client
  12. @app.route('/')
  13. def index():
  14. return '<h1>Welcome</h1>'
  15. @app.route('/get')
  16. def get_proxy():
  17. """Get a proxy
  18. """
  19. conn = get_conn()
  20. return conn.pop()
  21. @app.route('/count')
  22. def get_counts():
  23. """Get the count of proxies
  24. """
  25. conn = get_conn()
  26. return str(conn.queue_len)
  27. if __name__ == '__main__':
  28. app.run()