开鲁网站seo站长工具,建盏茶杯知识,wordpress 插件 漏洞,做暧暧视频免费视频中国网站你提供的代码是一个基本的Flask应用程序#xff0c;实现了一个简单的登录系统。以下是代码的详细解释#xff1a;
1. 导入必要的模块#xff1a;os 用于生成密钥#xff0c;Flask 用于创建Web应用程序。 2. 创建Flask应用程序的实例#xff0c;并为会话管理设置一个密钥。…你提供的代码是一个基本的Flask应用程序实现了一个简单的登录系统。以下是代码的详细解释
1. 导入必要的模块os 用于生成密钥Flask 用于创建Web应用程序。 2. 创建Flask应用程序的实例并为会话管理设置一个密钥。 3. 定义管理员的默认用户名和密码。 4. 定义登录路由/处理GET和POST请求。 - 如果请求方法是POST它会获取包含用户名和密码的JSON数据。 - 然后它会检查提供的用户名和密码是否与默认用户名和密码匹配。 - 如果凭据有效它会将用户名存储在会话中。
这个应用程序还包括其他几个路由 - /welcome 路由用于显示欢迎页面只处理GET请求。它会检查用户是否已登录如果已登录则显示欢迎页面否则重定向到登录页面。 - /home 路由用于显示主页只处理GET请求。它会检查用户是否已登录如果已登录则显示主页否则返回一个JSON响应表示无权限访问主页。 - /logout 路由用于注销用户它会清除会话数据并重定向到登录页面。
如果你运行这个应用程序它将在本地启动一个Web服务器并监听默认的端口通常是5000。你可以通过访问http://localhost:5000来访问登录页面。 项目结构 app.py
import os
from flask import Flask, render_template, request, redirect, url_for, session, jsonifyapp Flask(__name__)
app.secret_key os.urandom(24)# 默认的管理员账号和密码
default_username admin
default_password admin123# 路由登录页面
app.route(/, methods[GET, POST])
def login():if request.method POST:# 获取 JSON 数据data request.get_json()# 获取账号和密码username data.get(username)password data.get(password)# 验证账号和密码if username default_username and password default_password:# 将用户信息存储到 session 中session[username] usernamereturn jsonify({message: 登录成功})else:return jsonify({message: 账号或密码错误})return render_template(login.html)# 路由欢迎页面
app.route(/welcome, methods[GET])
def welcome():# 检查用户是否已经登录if username in session:return render_template(welcome.html, usernamesession[username])else:return redirect(url_for(login))# 路由主页
app.route(/home, methods[GET])
def home():# 检查用户是否已经登录if username in session:return render_template(home.html, usernamesession[username])else:return jsonify({message: 无权限访问主页})# 路由注销
app.route(/logout)
def logout():# 清除 session 数据session.pop(username, None)return redirect(url_for(login))if __name__ __main__:app.run()templates / login.html
!DOCTYPE html
html
headtitleLogin/title
/head
bodyh1Login/h1form idloginFormlabel forusernameUsername:/labelinput typetext idusername nameusername requiredbrlabel forpasswordPassword:/labelinput typepassword idpassword namepassword requiredbrbutton typesubmitLogin/button/formdiv idmessage/divscriptdocument.getElementById(loginForm).addEventListener(submit, function(event) {event.preventDefault();var username document.getElementById(username).value;var password document.getElementById(password).value;var data {username: username,password: password};fetch(/, {method: POST,headers: {Content-Type: application/json},body: JSON.stringify(data)}).then(response response.json()).then(data {document.getElementById(message).innerText data.message;if (data.message 登录成功) {window.location.href /welcome;}}).catch(error {console.error(Error:, error);});});/script
/body
/htmltemplates / welcome.html
!DOCTYPE html
html
headtitleWelcome/title
/head
bodyh1Welcome, {{ username }}!/h1pYou have successfully logged in./pa href/homeGo to Home/abra href/logoutLogout/a
/body
/htmltemplates / home.html
!DOCTYPE html
html
headtitleHome/title
/head
bodyh1Welcome, {{ username }}!/h1pThis is the home page./pa href/logoutLogout/a
/body
/htmltestcase.py
import requestssession requests.Session()def login():data {username: admin,password: admin123}url http://127.0.0.1:5000res session.request(urlurl, methodpost, jsondata)return sessiondef Welcome():url http://127.0.0.1:5000/welcomeres login().get(urlurl)print(res.text)def index_login():url http://127.0.0.1:5000/homeres login().request(urlurl, methodget)print(res.text)def index_notlogin():url http://127.0.0.1:5000/homeres requests.get(urlurl)print(res.json())