ProxyApi.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. """
  4. -------------------------------------------------
  5. File Name: ProxyApi.py
  6. Description :
  7. Author : JHao
  8. date: 2016/12/4
  9. -------------------------------------------------
  10. Change Activity:
  11. 2016/12/4:
  12. -------------------------------------------------
  13. """
  14. __author__ = 'JHao'
  15. import sys
  16. sys.path.append('../')
  17. from flask import Flask, jsonify, request
  18. from Util.GetConfig import GetConfig
  19. from Manager.ProxyManager import ProxyManager
  20. app = Flask(__name__)
  21. api_list = {
  22. 'get': u'get an usable proxy',
  23. # 'refresh': u'refresh proxy pool',
  24. 'get_all': u'get all proxy from proxy pool',
  25. 'delete?proxy=127.0.0.1:8080': u'delete an unable proxy',
  26. 'get_status': u'proxy statistics'
  27. }
  28. @app.route('/')
  29. def index():
  30. return jsonify(api_list)
  31. @app.route('/get/')
  32. def get():
  33. proxy = ProxyManager().get()
  34. return proxy if proxy else 'no proxy!'
  35. @app.route('/refresh/')
  36. def refresh():
  37. # TODO refresh会有守护程序定时执行,由api直接调用性能较差,暂不使用
  38. # ProxyManager().refresh()
  39. pass
  40. return 'success'
  41. @app.route('/get_all/')
  42. def getAll():
  43. proxies = ProxyManager().getAll()
  44. return jsonify(proxies)
  45. @app.route('/delete/', methods=['GET'])
  46. def delete():
  47. proxy = request.args.get('proxy')
  48. ProxyManager().delete(proxy)
  49. return 'success'
  50. @app.route('/get_status/')
  51. def getStatus():
  52. status = ProxyManager().getNumber()
  53. return jsonify(status)
  54. def run():
  55. config = GetConfig()
  56. app.run(host=config.host_ip, port=config.host_port)
  57. if __name__ == '__main__':
  58. run()