fix: 2023.6.1

1. 删去了wordCMD.py中:
from flask import ....,Blueprint,....
改为:
from flask import *
2. 修改了代码的格式,包括:等号两边的空格、加号两边的空格
3. 更新了访问用户单词的token验证
3.1 使用Authorization的字段值(Bearer xxx)验证token
3.2 取消了路由访问用户单词的功能,只能在终端命令行中输入: "curl -H "Authorization: Bearer 密钥" http://127.0.0.1:5000/show/用户名/"获取单词
SPM2023S-QianJunQi
poincareS 2023-06-04 10:41:24 +08:00
parent dc37f5f229
commit 2fb3003808
1 changed files with 22 additions and 20 deletions

View File

@ -1,37 +1,39 @@
from flask import Flask, request, Blueprint, render_template, json, jsonify from flask import *
from UseSqlite import InsertQuery, RecordQuery from flask_httpauth import HTTPTokenAuth
import pickle
import difficulty
import pickle_idea2
from Article import load_freq_history from Article import load_freq_history
from app import pickle_idea from wordfreqCMD import sort_in_descending_order
from app.wordfreqCMD import sort_in_descending_order import pickle_idea
auth = HTTPTokenAuth(scheme='Bearer')
path_prefix = '/var/www/wordfreq/wordfreq/' path_prefix = '/var/www/wordfreq/wordfreq/'
path_prefix = '../' # comment this line in deployment path_prefix = './' # comment this line in deployment
TKTK = 'token' # set token
show_bp = Blueprint( show_bp = Blueprint(
'site', 'site',
__name__, __name__,
) )
tokens = {
"token": "token"
}
@auth.verify_token
def verify_token(token):
if token in tokens:
return tokens[token]
@show_bp.route('/show/<name>/') # set route for show page <name> means the var name to search @show_bp.route('/show/<name>/') # set route for show page <name> means the var name to search
@auth.login_required
def show(name): def show(name):
token = request.args.get("token")
# when token is wrong
if token != TKTK:
return "token is wrong, please try again"
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (name) user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (name)
d = load_freq_history(user_freq_record) d = load_freq_history(user_freq_record)
freqlst = sort_in_descending_order(pickle_idea.dict2lst(d)) freqlst = sort_in_descending_order(pickle_idea.dict2lst(d))
print(freqlst) words_freq = [] # 存储单词表的数组,格式为 单词-词频
words_freq=[] # 存储单词表的数组,格式为 单词-词频
for i in range(len(freqlst)): for i in range(len(freqlst)):
words_freq.append(str(freqlst[i][0])+"-"+str(len(freqlst[i][1]))) words_freq.append(str(freqlst[i][0]) + "-" + str(len(freqlst[i][1])))
t={} t = {}
t[name]=words_freq t[name] = words_freq
return jsonify(t) return jsonify(t)