api.py 709 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from flask import Flask, g
  2. from .db import RedisClient
  3. __all__ = ['app']
  4. app = Flask(__name__)
  5. def get_conn():
  6. """
  7. Opens a new redis connection if there is none yet for the
  8. current application context.
  9. """
  10. if not hasattr(g, 'redis_client'):
  11. g.redis_client = RedisClient()
  12. return g.redis_client
  13. @app.route('/')
  14. def index():
  15. return '<h2>Welcome to Proxy Pool System</h2>'
  16. @app.route('/get')
  17. def get_proxy():
  18. """
  19. Get a proxy
  20. """
  21. conn = get_conn()
  22. return conn.pop()
  23. @app.route('/count')
  24. def get_counts():
  25. """
  26. Get the count of proxies
  27. """
  28. conn = get_conn()
  29. return str(conn.queue_len)
  30. if __name__ == '__main__':
  31. app.run()