forked from qiyeboy/IPProxyPool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunServer.py
186 lines (162 loc) · 6.53 KB
/
runServer.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# coding:utf-8
'''
定义几个关键字,count type,protocol,country,area,
API TEST:
'''
import os
BASE_DIR = os.path.dirname(__file__)
import sys
reload(sys)
sys.setdefaultencoding('utf8')
sys.path.append(BASE_DIR)
import json
from flask import Flask,abort,jsonify, globals,Blueprint, url_for, request,render_template,make_response
from flask_httpauth import HTTPBasicAuth
import config
from db.DataStore import sqlHelper
from util.filters import pretty_date
from db.SqlHelper import AlchemyEncoder,User
import pandas as pd
import chartkick
import logging
logger = logging.getLogger('api')
app = Flask(__name__)
auth = HTTPBasicAuth()
@app.route('/api/register', methods = ['POST'])
def regiser():
username = request.json.get('username')
password = request.json.get('password')
if username is None or password is None:
abort(400) # missing arguments
if sqlHelper.find_user(username) is not None:
abort(400) # existing user
sqlHelper.createUser(username,password)
return jsonify({ 'username': user.username }), 201, {'Location': url_for('get_user', id = user.id, _external = True)}
@app.route('/api/token')
@auth.login_required
def get_auth_token():
''' 产生授权码 '''
token = globals.user.generate_auth_token()
return jsonify({ 'token': token.decode('ascii') })
@auth.verify_password
def verify_password(username_or_token, password):
''' replacement to http_auth verify password'''
# first try to authenticate by token
user = User.verify_auth_token(username_or_token)
if not user:
# try to authenticate with username/password
user = sqlHelper.find_user(username_or_token)
if not user or not user.verify_password(password):
return False
globals.user = user
return True
@auth.error_handler
def unauthorized():
return make_response(jsonify({'error': 'Unauthorized access'}), 401)
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
@app.route("/")
def index():
def get_summary_data(proxys):
json_result = json.dumps(proxys,cls=AlchemyEncoder)
# load all data
df = pd.read_json(json_result)
df['score'] = df['score'].map({0:u'普通',1:u'高速'})
df['protocol'] = df['protocol'].map({0:u'http',1:u'https',2:u'all'})
# score
df_score = df.groupby(by='score')['ip'].count()
proxy_stats_by_score = df_score.to_json()
# country
df_country = df.groupby(by='country')['ip'].count()
proxy_stats_by_country = df_country.to_json()
# protocol
df_score = df.groupby(by='protocol')['ip'].count()
proxy_stats_by_protocol = df_score.to_json()
# 7 days trend
ret = sqlHelper.get_stats_7days_history()
df = pd.DataFrame(ret,columns=('updatetime','score','cnt'))
df['updatetime'] = df['updatetime'].astype('str')
df['score'] = df['score'].map({0:u'普通',1:u'高速'})
json_dict = []
for cat,cat_data in df.groupby('score'):
grp_dict = {}
grp_dict['name'] = cat
grp_dict['data'] = cat_data[['updatetime','cnt']].values.tolist()
json_dict.append(grp_dict)
proxy_his_stats = json.dumps(json_dict)
return proxy_stats_by_score,proxy_stats_by_country,proxy_stats_by_protocol,proxy_his_stats
inputs = request.args
proxys = sqlHelper.select(inputs.get('count', 800), inputs)
proxy_count = sqlHelper.query_count()
proxy_stats_by_score,proxy_stats_by_country,proxy_stats_by_protocol,proxy_his_stats = get_summary_data(proxys)
return render_template("index.html" ,proxys = proxys[:30], # 最多显示50条
proxy_stats_by_score=proxy_stats_by_score,
proxy_stats_by_country = proxy_stats_by_country,
proxy_stats_by_protocol = proxy_stats_by_protocol,
proxy_his_stats=proxy_his_stats,
proxy_count=proxy_count)
protocol_type = {0:u'http',1:u'https',2:u'all'}
@app.route("/api/proxy/normal")
@auth.login_required
def api_normal_proxys():
inputs = request.args
proxys = sqlHelper.select(inputs.get('count', None), {'score':0})
proxys_obj = [{'type':protocol_type[p.protocol],'addr':"%s:%d" % (p.ip, p.port)} for p in proxys]
json_result = json.dumps(proxys_obj)
return json_result
@app.route("/api/proxy/fast")
@auth.login_required
def api_fast_proxys():
inputs = request.args
proxys = sqlHelper.select(inputs.get('count', None),{'score':1})
proxys_obj = [{'type':protocol_type[p.protocol],'addr':"%s:%d" % (p.ip, p.port)} for p in proxys]
json_result = json.dumps(proxys_obj)
return json_result
@app.route("/api/proxy/public")
@auth.login_required
def api_public_proxys():
inputs = request.args
proxys = sqlHelper.select(count=50,conditions= {'score':0})
proxys_obj = [{'type':protocol_type[p.protocol],'addr':"%s:%d" % (p.ip, p.port)} for p in proxys]
json_result = json.dumps(proxys_obj)
return json_result
@app.route("/api/proxy/vip")
@auth.login_required
def api_vip_proxys():
'''
vip 方法可以返回全部对象信息,并可指定查询参数
'''
inputs = request.args
proxys = sqlHelper.select(inputs.get('count', None), inputs)
json_result = json.dumps(proxys,cls=AlchemyEncoder)
return json_result
@app.route("/api/proxy/china")
@auth.login_required
def api_china_proxys():
inputs = request.args
proxys = sqlHelper.select(inputs.get('count', None), {'country':u'国内','socre':1})
proxys_obj = [{'type':protocol_type[p.protocol],'addr':"%s:%d" % (p.ip, p.port)} for p in proxys]
json_result = json.dumps(proxys_obj)
return json_result
@app.route("/api/proxy/oversee")
@auth.login_required
def api_oversee_proxys():
inputs = request.args
proxys = sqlHelper.select(inputs.get('count', 10), {'country':u'国外','socre':1,'protocol':1})
proxys_obj = [{'type':protocol_type[p.protocol],'addr':"%s:%d" % (p.ip, p.port)} for p in proxys]
json_result = json.dumps(proxys_obj)
return json_result
@app.route("/api/delete")
def delete():
inputs = request.args
json_result = json.dumps(sqlHelper.delete(inputs),cls=AlchemyEncoder)
return json_result
# set the secret key. keep this really secret:
app.secret_key = config.APP_SECRET_KEY
app.static_folder = "static"
app.jinja_env.filters['pretty_date'] = pretty_date
ck = Blueprint('ck_page', __name__, static_folder=chartkick.js(), static_url_path='static')
app.register_blueprint(ck, url_prefix='/ck')
app.jinja_env.add_extension("chartkick.ext.charts")
app.run(port=config.API_PORT,debug=False)