-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.py
49 lines (41 loc) · 1.49 KB
/
api.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
import copy
import redis
from flask import Flask,Response,jsonify
import json
# encoding:utf-8
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
app = Flask(__name__)
returnModule = {"comment":"",
"nickName":"",
"songName":"",
"songId":"",
"likeCount":0,
"singer":""
}
redis_cli = redis.Redis(host="127.0.0.1", port=6379, decode_responses=True, db=2)
redis_cli_simple = redis.Redis(host="127.0.0.1", port=6379, decode_responses=True, db=1)
count = 0
@app.route('/get_comment')
def get_comment():
global count
count += 1
re_dict = copy.deepcopy(returnModule)
randomKey = redis_cli.randomkey()
randomCommentList = redis_cli.get(randomKey).split("|")
re_dict["likeCount"] = randomCommentList[0]
re_dict["nickName"] = randomCommentList[1]
re_dict["comment"] = randomCommentList[2]
re_dict["songName"] = randomCommentList[3]
re_dict["singer"] = randomCommentList[4]
re_dict["songId"] = randomKey.split("_")[0]
response = Response(json.dumps(re_dict), mimetype = 'application/json')
response.headers.add('Server','python flask')
print(re_dict)
return response
@app.route("/count")
def count_num():
return "请求数" + str(count)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80, debug = True)