forked from defnngj/learning-API-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_server.py
315 lines (252 loc) · 10.3 KB
/
api_server.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import json
import os
import base64
import hashlib
import time
from flask import Flask
from flask import jsonify
from flask import request
from flask import session
from flask import escape
from werkzeug.utils import secure_filename
from Crypto.Cipher import AES # 请安装 Crypto
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = BASE_PATH + '/uploads'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', "json"])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = 'please-generate-a-random-secret_key'
# 最简单的json格式返回
@app.route('/')
def hello_world():
return jsonify({"code": 10200, "message": "Welcome to API testing"})
# 通过 URI 传参
@app.route('/user/<username>')
def get_user(username):
msg = "hello, {}".format(username)
return jsonify({"code": 10200, "message": msg})
# 根据用户id返回数据
@app.route('/id/<int:uid>', methods=["GET", "POST"])
def get_uid(uid):
if request.method == "GET":
user_info = {"id": 1, "name": "tom", "age": 22}
if uid != 1:
response = {"code": 10101, "message": "user id null"}
else:
response = {"code": 10200, "message": "success", "data": user_info}
return jsonify(response)
else:
return jsonify({"code": 10101, "message": "request method error"})
# 一般的get传参方式,
@app.route("/search/", methods=["GET", "POST"])
def get_search():
if request.method == "GET":
search_word = request.args.get('q', '')
if search_word == "selenium":
data_list = ["selenium教程", "seleniumhq.org", "selenium环境安装"]
else:
data_list = []
return jsonify({"code": 10200, "message": "success", "data": data_list})
else:
return jsonify({"code": 10101, "message": "request method error"})
# post请求, from-data/x-www-from-urlencode参数方式
@app.route('/login', methods=["GET", "POST"])
def post_login():
if request.method == "POST":
username = request.form.get('username')
password = request.form.get('password')
if username is None or password is None:
response = {"code": 10102, "message": "username or passwrord is None"}
elif username == "" or password == "":
response = {"code": 10203, "message": "username or passwrord is null"}
elif username == "admin" and password == "a123456":
response = {"code": 10200, "message": "login success"}
else:
response = {"code": 10104, "message": "username or password error"}
return jsonify(response)
else:
return jsonify({"code": 10101, "message": "request method error"})
# post请求,json参数方式
@app.route('/add_user', methods=["GET", "POST"])
def post_add_user():
if request.method == "POST":
try:
data = json.loads(request.get_data())
except json.decoder.JSONDecodeError:
return jsonify({"code": 10105, "message": "format error"})
try:
name = data["name"]
age = data["age"]
height = data["height"]
except KeyError:
response = {"code": 10102, "message": "key null"}
else:
if name == "":
response = {"code": 10103, "message": "name null"}
elif name == "tom":
response = {"code": 10104, "message": "name exist"}
else:
data = {"name": name, "age": age, "height": height}
response = {"code": 10200, "message": "add success", "data": data}
return jsonify(response)
else:
return jsonify({"code": 10101, "message": "request method error"})
# 获取 header 信息处理
@app.route('/header', methods=['GET', 'POST'])
def post_header():
if request.method == 'POST':
token = request.headers.get("token")
ct = request.headers.get("Content-Type")
response = {"code": 10200, "message": "header ok!",
"data": {"token": token,"Content-Type": ct}}
return jsonify(response)
else:
return jsonify({"code": 10101, "message": "request method error"})
# Base Auth认证 ["Basic","YWRtaW46YWRtaW4xMjM="]
@app.route('/auth', methods=['GET', 'POST'])
def post_auth():
if request.method == 'POST':
auth = request.headers.get("Authorization")
if auth is None:
return jsonify({"code": 10101, "message": "Authorization None"})
else:
auth = auth.split()
auth_parts = base64.b64decode(auth[1]).decode('utf-8').partition(':')
userid, password = auth_parts[0], auth_parts[2]
if userid == "" or password == "":
return jsonify({"code": 10102, "message": "Authorization null"})
if userid == "admin" and password == "admin123":
return jsonify({"code": 10200, "message": "Authorization success!"})
else:
return jsonify({"code": 10103, "message": "Authorization fail!"})
else:
return jsonify({"code": 10101, "message": "request method error"})
# 文件后缀名的判断
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
# 文件上传的接口
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
if f and allowed_file(f.filename):
filename = secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
response = {"code": 10200, "message": "upload success!"}
else:
response = {"code": 10102, "message": "file type error!"}
return jsonify(response)
else:
return jsonify({"code": 10101, "message": "request method error"})
# 一个URL, 根据不同的方法做不同的处理
@app.route('/phone/<int:pid>', methods=['GET', 'POST', "PUT", "DELETE"])
def more_used(pid):
if request.method == 'GET':
if pid == 1:
phone_info = {
"id": pid,
"name": "小米手机",
"price": 1999
}
response = {"code": 10201, "message": "get success", "data": phone_info}
else:
response = {"code": 10101, "message": "The phone id is empty"}
return jsonify(response)
elif request.method == "PUT":
if pid == 1:
name = request.form.get('name')
price = request.form.get('price')
phone_info = {
"id": pid,
"name": name,
"price": price
}
response = {"code": 10202, "message": "update success", "data": phone_info}
else:
response = {"code": 10102, "message": "The updated phone id is empty"}
return jsonify(response)
elif request.method == "DELETE":
if pid == 1:
response = {"code": 10203, "message": "delete success"}
else:
response = {"code": 10103, "message": "The deleted phone id is empty"}
return jsonify(response)
# 通过Session 记录登录状态
@app.route("/user_login", methods=['POST'])
def session_login():
username = request.form['username']
password = request.form['password']
if username != "" and password != "":
session['username'] = username
return jsonify({"code": 10200, "message": 'login success'})
else:
return jsonify({"code": 10200, "message": 'login faile'})
@app.route("/user_data")
def session_user_data():
if 'username' in session:
username = format(escape(session['username']))
return jsonify({"code": 10200, "message": 'hello, {}'.format(username)})
return jsonify({"code": 10200, "message": 'hello, stranger'})
# 签名的接口 时间戳+ MD5加密
@app.route('/sign/', methods=['GET', 'POST'])
def post_sign():
if request.method == 'POST':
client_time = request.form.get('time') # 客户端时间戳
client_sign = request.form.get('sign') # 客户端签名
if client_time is None or client_sign is None:
return jsonify({"code": 10102, "message": "time or sign is None"})
elif client_time == "" or client_sign == "":
return jsonify({"code": 10103, "message": "time or sign is null"})
else:
# 服务器时间
now_time = time.time() # 例:1466426831
server_time = str(now_time).split('.')[0]
# 获取时间差
time_difference = int(server_time) - int(client_time)
if time_difference >= 60:
return jsonify({"code": 10104, "message": "timeout"})
# 签名检查
md5 = hashlib.md5()
sign_str = client_time + "&Guest-Bugmaster"
sign_bytes_utf8 = sign_str.encode(encoding="utf-8")
md5.update(sign_bytes_utf8)
sever_sign = md5.hexdigest()
if sever_sign != client_sign:
return jsonify({"code": 10105, "message": "sign error!"})
else:
return jsonify({"code": 10200, "message": "sign success!", "data": []})
else:
return jsonify({"code": 10101, "message": "request method error"})
############ AES加密的接口 #############
BS = 16
def unpad(s): return s[0: - ord(s[-1])]
def decryptBase64(src):
return base64.urlsafe_b64decode(src)
def decryptAES(src):
"""
解析AES密文
"""
src = decryptBase64(src)
key = b'W7v4D60fds2Cmk2U'
iv = b"1172311105789011"
cryptor = AES.new(key, AES.MODE_CBC, iv)
text = cryptor.decrypt(src).decode()
return unpad(text)
@app.route('/aes/', methods=['GET', 'POST'])
def post_aes():
if request.method == 'POST':
data = request.form.get('data') # AES加密的数据
if data is None or data == "":
return jsonify({"code": 10102, "message": "data is None"})
# 解密
decode = decryptAES(data)
# 转化为字典
dict_data = json.loads(decode)
return jsonify({"code": 10200, "message": "success", "data": dict_data})
else:
return jsonify({"code": 10101, "message": "request method error"})
############ end #############
if __name__ == '__main__':
app.debug = True
app.run()