[python]代码库
from flask import Flask, session, redirect, url_for, request
app = Flask(__name__)
app.secret_key = "secret_key"
@app.route("/login", methods=["POST"])
def login():
username = request.form["username"]
password = request.form["password"]
if username == "admin" and password == "password":
session["logged_in"] = True
else:
session["logged_in"] = False
return redirect(url_for("home"))
@app.route("/logout")
def logout():
session["logged_in"] = False
return redirect(url_for("home"))
@app.route("/")
def home():
if not session.get("logged_in"):
return redirect(url_for("login"))
else:
return "Welcome, admin!"
if __name__ == "__main__":
app.run()