1
0

api.py 777 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from flask import Flask, g
  2. from .db import RedisClient
  3. from .setting import API_HOST, API_PORT
  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. return '<h2>Welcome to Proxy Pool System</h2>'
  17. @app.route('/random')
  18. def get_proxy():
  19. """
  20. get a random proxy
  21. :return: 随机代理
  22. """
  23. conn = get_conn()
  24. return conn.random()
  25. @app.route('/count')
  26. def get_counts():
  27. """
  28. get the count of proxies
  29. :return: 代理池总量
  30. """
  31. conn = get_conn()
  32. return str(conn.count())
  33. if __name__ == '__main__':
  34. app.run(host=API_HOST, port=API_PORT, threaded=True)