题目
利用Flask和Sqlite模块,编写相应的Python程序与网页代码,来模拟用户登录验证过程。功能是:运行Python程序,启动浏览器并输入相应网址,在网页中输入用户名、密码,与数据库中的数据进行比对,若正确则在浏览器中显示成功信息,否则显示“用户名或密码错误!”。存储用户名、密码的数据表中共有三个字段,依次表示序号、用户名、密码。input.html网页与success.html网页内容如下:
<html><head><title>输入账号密码</title></head>
<body>
<form
action="/deal_request" method="get">
请输入账号:<input type="text" name="usr"
><br>
请输入密码:<input type="password"
name="psd"><br> <input type="submit"
value="提交"
/>
</form>
</body></html>
<html><head><title>Welcome</title></head>
<body>
<h1>登录成功!</h1><br>
<h1>欢迎你: ① </h1><br>
</body></html>
编写的 Python 程序如下:
from flask import
Flask, render_template, request
import sqlite3
②
= Flask(_name_)
@app.route('/')
def input():
return render_template('input.html')
@app.route('/deal_request',
methods = ['GET'])
def deal_request():
get_usr =
request.args.get('usr')
get_psd =
request.args.get('psd')
if check(get_usr, get_psd):
return render_template('succes.html',
name=get_usr)
else:
return '用户名或密码错误!'
def check(name, psd):
db=sqlite3.connect('login.db')
cur= ③
#创建游标对象
cur.execute('select
* from users')
data=cur.fetchall()
for rec in data: #比对用户名与密码
if rec[ 1]==name and rec[2]==psd:
return True
else:
return False
if _name_ == '_main_':
app.run(host='
127.0.0. 1 ', port=5000, debug=False)
请完成下列题目:
(1)
在浏览器中输入地址:http://127.0.0. 1:5000 ,则访问的网页是。
(2)
若登录成功后,在浏览器中文字“欢迎你:”的后面显示当前用户名,则划线①处的代码是 。
(3)
要实现上述功能,完善划线②、③处代码。
②③
(4)
若本机IP地址为192.168.0.1,若要使同网段内其他计算机可以访问上述服务,则需将加框处的IP地址改为。
答案: 【1】input. html
【1】{{ name }}
【1】app【2】db.cursor0
【1】192.168.0.1