使用Flask框架搭建一个简易的登录界面,登录成功获取token数据
1 搭建简易登录界面
代码如下
from flask import Flask, jsonify
from flask import request
import time, hashlibapp = Flask(__name__)login_html = '''
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action="/doLogin" method="post">Account:<input type="text" name="account"><br>PassWord:<input type="text" name="password"><br>
<input type="submit" value="Submit">
<input type="reset" value="reset">
</form>
</body>
</html>
'''@app.route('/', methods=['GET', 'POST'])
def login_index():return login_html@app.route('/doLogin', methods=['POST'])
def do_login():if request.method == 'POST':account = request.form['account']password = request.form['password']if account == 'freePHP' and password == '123456':timestamp = time.time()prev_str = account + password +str(timestamp)token = hashlib.md5(prev_str.encode(encoding='UTF-8')).hexdigest()json_data = [{'token': token, 'user_id':101}];return jsonify({'data':json_data, 'result':True, 'errorMsg':''})else:return jsonify({'data':[], 'result':True, 'errorMsg':'Account and password is not matched'})if __name__ == '__main__':app.run(debug=True)
启动
浏览器访问http://127.0.0.1:5000
登录账户密码正确,获取token
登录账户密码输入错误:
后续操作:登录界面连接上数据库,判断是否能登录等一系列操作
注意点
问题:在进行提交密码时遇到Not Found,经查询是登录路由编写错误
改为:@app.route('/doLogin', methods=['POST'])查看资料:flask 不能访问/login_the requested url was not found on the server. if -CSDN博客
flask - The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again - Stack Overflow