需要完成目标
(1)利用MySQL数据库完成注册、登录、修改密码等操作(重构)
(2)记录用户操作的历史记录(登录、注册、改密、改电话)。
前置
import re
import pymysql
from pymysql.cursors import DictCursor
username_pat=r'[a-zA-Z]{1}[a-zA-Z0-9]{4,11}'
password_pat="(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]{6,15}"
phone_pat=r'1[3-9]\d{9}'
菜单
def menu():
while True:
print('*'*35)
print(
"1.登录"
"2.注册"
"3.修改密码"
"4.修改电话"
"5.退出"
"6.查看历史记录(仅管理员用户)"
)
optin=input("请选则你要进行的服务(输入前面编号即可):")
if optin=='1':
log_in()
elif optin=='2':
sig_in()
elif optin=='3':
updata_password()
elif optin=='4':
updata_phone()
elif optin=='5':
exit()
elif optin=='6':
read_history()
else:print("请输入正确编号")
登录
def log_in():
username=input('输入用户名:')
password=input('输入密码:')
num=0
while num<2:
if log_in_mysql(username,password):
print('登录成功')
action='log_in,0'
history_mysql(action,username)
return
else:
num=num+1
print(f'错误{num}次')
username = input('输入用户名:')
password = input('输入密码:')
print('错误次数太多,返回主菜单')
action='log_in,1'
history_mysql(action,username)
注册
def sig_in():
username=input('请输入用户名:')
while not re.fullmatch(username_pat, username) or not pre_updata_mysql_username(username):
print('用户名不符合规则')
username = input('请输入用户名:')
else:
password = input('请输入密码:')
while not re.fullmatch(password_pat, password):
print('密码设定不符合规则')
password = input('请输入密码:')
else:
phone = input('请输入手机号码:')
while not re.fullmatch(phone_pat, phone) or not pre_updata_mysql_tel(phone):
print('电话不符合规则')
phone = input('请输入手机号码:')
else:
if sig_in_mysql(username,password,phone):
print('注册成功')
action='sig_in,0'
history_mysql(action,username)
else:
print('用户名或号码以存在')
action='sig_in,1'
history_mysql(action,username)
修改密码
def updata_password():
print('请先登录')
username=input('输入用户名:')
password=input('输入密码:')
num = 0
while num<2:
if not log_in_mysql(username,password):
num=num+1
print(f'错误{num}次')
username = input('输入用户名:')
password = input('输入密码:')
else:
password=input('请输入新密码:')
while not re.fullmatch(password_pat, password):
print('密码设定不符合规则')
password = input('输入新密码:')
else:
passwords=input('确认密码:')
if password == passwords:
if updata_mysql_password(username,password):
print('修改成功')
action='updata_password,0'
history_mysql(action,username)
break
else:
print('修改失败')
action='updata_password,1'
history_mysql(action,username)
break
else:
print('两次密码不一致')
action=1
history_mysql(action,username)
break
修改电话
def updata_phone():
print('请先登录')
username=input('输入用户名:')
password=input('输入密码:')
num = 0
while num<2:
if not log_in_mysql(username,password):
num=num+1
print(f'错误{num}次')
username = input('输入用户名:')
password = input('输入密码:')
else:
phone =input('输入新电话号码:')
while not re.fullmatch(phone_pat, phone):
print('电话号码不符合规则')
phone = input('输入新号码:')
else:
if not pre_updata_mysql_tel(phone):
print('该号码以注册')
action='updata_phone,0'
history_mysql(action,username)
break
else:
if updata_mysql_tel(username,phone):
print('修改成功')
action='updata_phone,1'
history_mysql(action,username)
break
读取历史操作记录
def read_history():
print('管理员登录仅有一次机会')
username=input('请输入用户名:')
password=input('请输入密码:')
con = pymysql.connect(host='192.168.18.141', user='root', password='123456', port=3306, database='blog',
charset='utf8mb4')
curses = con.cursor(DictCursor)
sql = f"select role from users where username='{username}' and password='{password}';"
curses.execute(sql)
res = curses.fetchone()
for r in res.values():
# print(r)
if r=='vip':
read_history_mysql()
print(read_history_mysql())
action = 'read_history,1'
history_mysql(action, username)
break
else:
print('登录失败')
action='read_history,1'
history_mysql(action,username)
break
后端支持
登录校验
def log_in_mysql(username,password):
con = pymysql.connect(host='192.168.18.141', user='root', password='123456', port=3306, database='blog',
charset='utf8mb4') #是自己mysql库
curses = con.cursor()
sql=f"select * from users where username='{username}' and password='{password}';"
res = curses.execute(sql)
curses.close()
con.close()
if res==1:
# print(1)
return True
else:
# print(2)
return False
注册操作
def sig_in_mysql(username,password,phone):
con = pymysql.connect(host='192.168.18.141', user='root', password='123456', port=3306, database='blog',
charset='utf8mb4')
curses = con.cursor()
sql=f"insert users(username,password,tel) values('{username}','{password}','{phone}')"
res=curses.execute(sql)
con.commit()
curses.close()
con.close()
if res==1:
return True
else:
return False
修改密码后端
def updata_mysql_password(username,passswrod):
con = pymysql.connect(host='192.168.18.141', user='root', password='123456', port=3306, database='blog',
charset='utf8mb4')
curses = con.cursor()
sql=f"update users set password='{passswrod}' where username='{username}'"
res = curses.execute(sql)
con.commit()
curses.close()
con.close()
if res == 1:
return True
else:
return False
修改号码后端
def updata_mysql_tel(username,phone):
con = pymysql.connect(host='192.168.18.141', user='root', password='123456', port=3306, database='blog',
charset='utf8mb4')
curses = con.cursor()
sql = f"update users set tel='{phone}' where username='{username}'"
res = curses.execute(sql)
con.commit()
curses.close()
con.close()
if res == 1:
return True
else:
return False
校验号码是否存有(注册,修改号码使用)
def pre_updata_mysql_tel(phone):
con = pymysql.connect(host='192.168.18.141', user='root', password='123456', port=3306, database='blog',
charset='utf8mb4')
curses = con.cursor()
sql = f"select * from users where tel='{phone}';"
res = curses.execute(sql)
curses.close()
con.close()
if res==1:
return False
else:
return True
校验用户名是否存在
def pre_updata_mysql_username(username):
con = pymysql.connect(host='192.168.18.141', user='root', password='123456', port=3306, database='blog',
charset='utf8mb4')
curses = con.cursor()
sql = f"select * from users where username='{username}';"
res = curses.execute(sql)
curses.close()
con.close()
if res==1:
return False
else:
return True
记录操作
def history_mysql(action,username):
con = pymysql.connect(host='192.168.18.141', user='root', password='123456', port=3306, database='blog',
charset='utf8mb4')
curses = con.cursor()
sql = f"insert user_history(username,action) values('{username}','{action}')"
curses.execute(sql)
con.commit()
curses.close()
con.close()
读取历史操作后端
def read_history_mysql():
con = pymysql.connect(host='192.168.18.141', user='root', password='123456', port=3306, database='blog',
charset='utf8mb4')
curses = con.cursor(DictCursor)
sql="select * from user_history"
curses.execute(sql)
res = curses.fetchone()
curses.close()
con.close()
return res