forked from jhao104/proxy_pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxyApi.py
93 lines (66 loc) · 1.93 KB
/
ProxyApi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
# !/usr/bin/env python
"""
-------------------------------------------------
File Name: ProxyApi.py
Description :
Author : JHao
date: 2016/12/4
-------------------------------------------------
Change Activity:
2016/12/4:
-------------------------------------------------
"""
__author__ = 'JHao'
import sys
from werkzeug.wrappers import Response
from flask import Flask, jsonify, request
sys.path.append('../')
from Util.GetConfig import GetConfig
from Manager.ProxyManager import ProxyManager
app = Flask(__name__)
class JsonResponse(Response):
@classmethod
def force_type(cls, response, environ=None):
if isinstance(response, (dict, list)):
response = jsonify(response)
return super(JsonResponse, cls).force_type(response, environ)
app.response_class = JsonResponse
api_list = {
'get': u'get an usable proxy',
# 'refresh': u'refresh proxy pool',
'get_all': u'get all proxy from proxy pool',
'delete?proxy=127.0.0.1:8080': u'delete an unable proxy',
'get_status': u'proxy statistics'
}
@app.route('/')
def index():
return api_list
@app.route('/get/')
def get():
proxy = ProxyManager().get()
return proxy if proxy else 'no proxy!'
@app.route('/refresh/')
def refresh():
# TODO refresh会有守护程序定时执行,由api直接调用性能较差,暂不使用
# ProxyManager().refresh()
pass
return 'success'
@app.route('/get_all/')
def getAll():
proxies = ProxyManager().getAll()
return proxies
@app.route('/delete/', methods=['GET'])
def delete():
proxy = request.args.get('proxy')
ProxyManager().delete(proxy)
return 'success'
@app.route('/get_status/')
def getStatus():
status = ProxyManager().getNumber()
return status
def run():
config = GetConfig()
app.run(host=config.host_ip, port=config.host_port)
if __name__ == '__main__':
run()