Python模拟登录成功与失败处理方式(不涉及前端)
任务说明:
(1) 用户输入用户名,如不存在此用户不能登录;
(2) 用户在输入密码时,如果连续输入三次错误,则该用户被锁定一段时间;
(3) 用户被锁定一段时间后,可再次进行尝试登录;
程序使用库:
datetime https://docs.python.org/2/library/datetime.html
pymysql http://pymysql.readthedocs.io/en/latest/index.html
数据库设计如下:
数据库数据形式如下
程序说明:
程序结构为顺序结构,未涉及到函数的调用之类,只是为了练习使用pymysql 进行增删该查的功能
整体思路如下
(1) 链接数据库
1 #创建数据库的链接 2 connection = pymysql.connect(host='127.0.0.1', 3 port=3306, 4 user='root', 5 password='123456', 6 db='myschool', 7 charset='utf8', 8 cursorclass=pymysql.cursors.DictCursor)
(2) 交互输入登陆用名名,并根据输入查询数据库数据
1 userName = input("输入用户名:") 2 if "q"!=userName and "Q"!=userName: 3 with connection.cursor() as cursor: 4 #根据用户名查出用户信息 5 sql = 'select * from t_user where name = %s' 6 result = cursor.execute(sql,userName);
(3) 判断输入的用户名是否存在,如不存在则返回重新输入,如存在则判断是否被锁定,如果被锁定还得判断是否已经过了锁定时间
1 # 判断是否存在此用户 2 if 0==result: 3 print("无此用户名存在!") 4 continue 5 #获取用户信息 6 item = cursor.fetchone() 7 is_login = False 8 # 判断用户是否被禁 9 if item["is_based"]: 10 last_login_time = item["login_time"] 11 login_time = datetime.datetime.now() 12 waiting_time = int(((login_time-last_login_time).total_seconds())/60) 13 #用户如果被禁,判断还需要多长时间等待 14 if (waiting_time-3)<0: 15 print("账号暂时锁定,请等待%d分钟"%(3-waiting_time)) 16 break
(4) 连续输入三次,如果密码输入错则被登录失败,否则登陆成功
1 times = 0 2 # 3次输入密码的机会 3 while times<3: 4 password = input("请输入密码:") 5 if password != item["password"]: 6 times += 1 7 else: 8 is_login = True 9 break 10 11 # 登录成功与否,禁用信息以及登录信息数据都在数据库进行更新 12 if is_login: 13 is_based = 0 14 # 将datetime转换字符串类型 15 login_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 16 print("欢迎%s,登陆成功!"%(userName)) 17 else: 18 is_based = 1 19 login_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 20 print("登录失败,请等待10min")
(4) 更新数据库的信息
1 # 更新数据库的Mysql语句 2 sql = "update t_user set is_based = %d , login_time = '%s' where name='%s'"%(is_based,login_time,userName) 3 cursor.execute(sql) 4 5 # 由于对数据库进行了更新,故需要提交事务 6 connection.commit()
全部代码如下:
1 import pymysql 2 import datetime 3 #创建数据库的链接 4 connection = pymysql.connect(host='127.0.0.1', 5 port=3306, 6 user='root', 7 password='123456', 8 db='myschool', 9 charset='utf8', 10 cursorclass=pymysql.cursors.DictCursor) 11 try: 12 while True: 13 userName = input("输入用户名:") 14 if "q"!=userName and "Q"!=userName: 15 with connection.cursor() as cursor: 16 #根据用户名查出用户信息 17 sql = 'select * from t_user where name = %s' 18 result = cursor.execute(sql,userName); 19 # 判断是否存在此用户 20 if 0==result: 21 print("无此用户名存在!") 22 continue 23 #获取用户信息 24 item = cursor.fetchone() 25 is_login = False 26 # 判断用户是否被禁 27 if item["is_based"]: 28 last_login_time = item["login_time"] 29 login_time = datetime.datetime.now() 30 waiting_time = int(((login_time-last_login_time).total_seconds())/60) 31 #用户如果被禁,判断还需要多长时间等待 32 if (waiting_time-3)<0: 33 print("账号暂时锁定,请等待%d分钟"%(3-waiting_time)) 34 break 35 36 times = 0 37 # 3次输入密码的机会 38 while times<3: 39 password = input("请输入密码:") 40 if password != item["password"]: 41 times += 1 42 else: 43 is_login = True 44 break 45 46 # 登录成功与否,禁用信息以及登录信息数据都在数据库进行更新 47 if is_login: 48 is_based = 0 49 # 将datetime转换字符串类型 50 login_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 51 print("欢迎%s,登陆成功!"%(userName)) 52 else: 53 is_based = 1 54 login_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 55 print("登录失败,请等待10min") 56 57 # 更新数据库的Mysql语句 58 sql = "update t_user set is_based = %d , login_time = '%s' where name='%s'"%(is_based,login_time,userName) 59 cursor.execute(sql) 60 61 # 由于对数据库进行了更新,故需要提交事务 62 connection.commit() 63 break 64 else: 65 print("退出!!!") 66 break 67 finally: 68 # 最终关闭链接 69 connection.close();