# # 需求: 设计一个程序 要求用户可以实现登录、注册、注销登录等功能
# # 用户的用户名(长度6-20)、密码(长度8-20)、昵称、年龄信息保存到数据库中
# # 维护用户的登录状态,每次只能有一个用户处于登录状态,直到选择注销登录
# # 如果不选择退出系统,该程序应当一直运行
import pymysql
import hashlib
from action import *
import re
# 数据库增加功能
def adduser(username, password, name, age):
try:
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', db='user',
charset='gbk')
cus1 = conn.cursor()
sql = "insert into tb_user(username,password,name,age) VALUES (%s,%s,%s,%s)"
res = [username, password, name, age]
cus1.execute(sql, res)
conn.commit()
return True
except Exception as e:
print(e)
finally:
cus1.close()
conn.close()
# 数据库查找功能
def finduser(username,password):
try:
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', db='user',
charset='gbk')
cus1 = conn.cursor()
sql = "select * from tb_user where username=%s"
res = [username]
cus1 = conn.cursor()
cus1.execute(sql, res)
psw = cus1.fetchall()
if psw == ():
return 0
elif psw[0][0] == password:
return 1
else:
return 2
except Exception as e:
print(e)
finally:
cus1.close()
conn.close()
# 数据库查询登录状态功能
def findstatus(username):
try:
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', db='user',
charset='gbk')
cus1 = conn.cursor()
sql = "select status from tb_user where username=%s"
res = [username]
cus1 = conn.cursor()
cus1.execute(sql, res)
psw = cus1.fetchall()
if psw[0][0] != 0:
return 1
else:
return 0
except Exception as e:
print(e)
finally:
cus1.close()
conn.close()
# 数据库变更登录状态功能
def updatestatus0(username):
try:
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', db='user',
charset='gbk')
cus1 = conn.cursor()
sql = "update tb_user set status=0 where username=%s"
res = [username]
cus1 = conn.cursor()
psw = cus1.execute(sql, res)
conn.commit()
if psw <= 0:
return False
else:
return True
except Exception as e:
print(e)
finally:
cus1.close()
conn.close()
# 数据库变更登录状态功能
def updatestatus1(username):
try:
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', db='user',
charset='gbk')
cus1 = conn.cursor()
sql = "update tb_user set status=1 where username=%s"
res = [username]
cus1 = conn.cursor()
psw = cus1.execute(sql, res)
conn.commit()
if psw <= 0:
return False
else:
return True
except Exception as e:
print(e)
finally:
cus1.close()
conn.close()
# 注册功能
def doregist():
username = input("请输入用户名:")
password = input("请输入密码:")
name = input("请输入昵称:")
age = input("请输入年龄:")
result_name = re.compile(r"^[a-zA-Z]\w{8,20}")
result_password = re.compile(r"^[a-zA-Z]\w{6,20}")
if result_name.match(username) and result_password.match(password):
regist(username,password,name,age)
else:
print("用户名(8-20)或密码(6-20)不合格")
doregist()
def regist(username,password,name,age):
if adduser(username,password,name,age):
print("注册成功,请登录")
dologin()
else:
print("注册失败")
welcome()
# 登录功能
def dologin():
username = input("请输入用户名:")
password = input("请输入密码:")
login(username,password)
def login(username,password):
result = finduser(username,password)
if result == 0:
print("用户名不存在")
welcome()
elif result == 1:
check(username)
else:
print("密码不对")
welcome()
# 检查登录状态功能
def check(username):
if findstatus(username) == 1:
print("用户已登录")
welcome()
else:
if updatestatus1(username):
print("登录成功")
loginsuccess(username)
else:
print("登录状态更新失败")
# 登录成功
def loginsuccess(username):
print("登录成功,",username)
input("按回车注销登录")
updatestatus0(username)
welcome()
# 欢迎界面
def welcome():
choose = input("请登录或注册(输入登录即登录,输入注册即注册)\r\n")
if choose != "":
if choose == "登录":
dologin()
else:
if choose == "注册":
doregist()
else:
welcome()
else:
welcome()
# 入口
print("欢迎进入学生信息管理系统")
welcome()


# 下面为数据库的结构
