在上一篇博文中简单的登录页面程序上 在做一些升级 增加了SESSION的记录 用来记录登录状态,增加了 密码用MD5加密
#演示使用Mongodb数据库简单的登录页面 #作者:Xiaoter #Date:2021-04-03 from pymongo import MongoClient from flask import Flask,request,render_template from flask import Blueprint from flask import session #from flask import request #from flask import render_template from hashlib import md5 from mydb import mydb import os from datetime import timedelta con = MongoClient('127.0.0.1',27017) db = con.mydb myrs = db.user app = Flask(__name__) app.register_blueprint(mydb) app.config["SECRET_KEY"]=os.urandom(24) app.config["PERMANENT_SESSION_LIFETIME"]=timedelta(days=7) @app.route('/') def index(): if session.get("username") !=None: return session["username"] else: return render_template('login.html') @app.route("/logout") def logout(): session.pop("username") session.clear(); return "CLEAR SESSION" @app.route("/checklogin",methods=['post']) def checklogin(): uname= request.form['username'] pwd=request.form["pwd"] pwd = md5(pwd.encode("utf8")).hexdigest()#密码md5加密 print(pwd) x=myrs.find_one({"username":uname}) if x==None: return "用户名或密码错误" else: if pwd==x["pwd"]: session["username"] = uname#设置Session return "登录成功" else: return "用户名或密码错误,请重新登录" if __name__=="__main__": app.debug=True app.run()