api.py 636 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. :return: 随机代理
  17. """
  18. conn = get_conn()
  19. return conn.random()
  20. @app.route('/count')
  21. def get_counts():
  22. """
  23. Get the count of proxies
  24. :return: 代理池总量
  25. """
  26. conn = get_conn()
  27. return str(conn.count())
  28. if __name__ == '__main__':
  29. app.run()