ProxyApi.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. from flask import Flask, jsonify, request
  17. from Util.GetConfig import GetConfig
  18. sys.path.append('../')
  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. }
  27. @app.route('/')
  28. def index():
  29. return jsonify(api_list)
  30. @app.route('/get/')
  31. def get():
  32. proxy = ProxyManager().get()
  33. return proxy
  34. @app.route('/refresh/')
  35. def refresh():
  36. # TODO refresh会有守护程序定时执行,由api直接调用性能较差,暂不使用
  37. # ProxyManager().refresh()
  38. pass
  39. return 'success'
  40. @app.route('/get_all/')
  41. def getAll():
  42. proxies = ProxyManager().getAll()
  43. return jsonify([proxy.decode('utf8') for proxy in proxies])
  44. @app.route('/delete/', methods=['GET'])
  45. def delete():
  46. proxy = request.args.get('proxy')
  47. ProxyManager().delete(proxy)
  48. return 'success'
  49. @app.route('/get_status/')
  50. def get_status():
  51. status = ProxyManager().get_status()
  52. return jsonify(status)
  53. def run():
  54. config = GetConfig()
  55. app.run(host=config.host_ip, port=config.host_port)
  56. if __name__ == '__main__':
  57. run()