ProxyApi.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 werkzeug.wrappers import Response
  17. from flask import Flask, jsonify, request
  18. sys.path.append('../')
  19. from Config.ConfigGetter import config
  20. from Manager.ProxyManager import ProxyManager
  21. app = Flask(__name__)
  22. class JsonResponse(Response):
  23. @classmethod
  24. def force_type(cls, response, environ=None):
  25. if isinstance(response, (dict, list)):
  26. response = jsonify(response)
  27. return super(JsonResponse, cls).force_type(response, environ)
  28. app.response_class = JsonResponse
  29. api_list = {
  30. 'get': u'get an useful proxy',
  31. # 'refresh': u'refresh proxy pool',
  32. 'get_all': u'get all proxy from proxy pool',
  33. 'delete?proxy=127.0.0.1:8080': u'delete an unable proxy',
  34. 'get_status': u'proxy number'
  35. }
  36. @app.route('/')
  37. def index():
  38. return api_list
  39. @app.route('/get/')
  40. def get():
  41. proxy = ProxyManager().get()
  42. return proxy.info_json if proxy else {"code": 0, "src": "no proxy"}
  43. @app.route('/refresh/')
  44. def refresh():
  45. # TODO refresh会有守护程序定时执行,由api直接调用性能较差,暂不使用
  46. # ProxyManager().refresh()
  47. pass
  48. return 'success'
  49. @app.route('/get_all/')
  50. def getAll():
  51. proxies = ProxyManager().getAll()
  52. return [_.info_dict for _ in proxies]
  53. @app.route('/delete/', methods=['GET'])
  54. def delete():
  55. proxy = request.args.get('proxy')
  56. ProxyManager().delete(proxy)
  57. return {"code": 0, "src": "success"}
  58. @app.route('/get_status/')
  59. def getStatus():
  60. status = ProxyManager().getNumber()
  61. return status
  62. def run():
  63. app.run(host=config.host_ip, port=config.host_port)
  64. if __name__ == '__main__':
  65. run()