Python 文件读写,条件循环(三次登录锁定账号实例)
通过文件读写,条件循环相关语法,实现三次登录失败则锁定该账号的功能
需求一
"""
需求描述:
1.输入正确账号,密码,退出程序
2.登录失败,重新输入账号密码
3.同一账号连续登录失败超过三次,则锁定该账号
4.登录已锁定账号,提示账号已锁定
"""
流程图一
实例代码一
#!/usr/bin/python #coding=utf-8 """ 需求描述: 1.输入正确账号,密码,退出程序 2.登录失败,重新输入账号密码 3.同一账号连续登录失败超过三次,则锁定该账号 4.登录已锁定账号,提示账号已锁定 user.txt: Milton Loveyp 0 Cherish Loveypgfc 0 yangp Loveypgfc 0 """ def modify_user(name, mode="add"): """ 修改用户名单中的登录错误次数 :param name: 用户名称 :param mode:add,登录错误次数自增1;del,登录错误次数重置为0 :return: """ # 读取用户文件 with open("user.txt", "r") as read_file: read_data = read_file.readlines() # 写入用户文件 with open("user.txt", "w") as write_file: for line in read_data: new_line = line.split() if new_line[0] == name: if mode == "add": new_line[2] = bytes(int(new_line[2]) + 1) else: new_line[2] = "0" new_line = " ".join(new_line) + "\n" write_file.write(new_line) write_file.flush() else: write_file.write(line) def auth(name, passwd): """ 用户鉴权 :param name: 登录用户名 :param passwd: 登录密码 :return: """ with open("user.txt", "r") as read_file: for eachLine in read_file: user_name, password, errors = eachLine.split() if name == user_name: if int(errors) < 3: if password == passwd: print "Dear [%s],welcome~" % name modify_user(name, "del") return True else: print "auth fail,please check your account and password!" modify_user(name, "add") return False else: print "your accout is locked!" return False else: print "your account is not exist!" return False def login(): """ 登录 :return: """ while True: name = raw_input("Name:") passwd = raw_input("PASS:") if auth(name, passwd): break if __name__ == '__main__': login()
需求二,在一的基础上添加锁定时间控制。
"""
需求点:
1.用户输入账号与密码
2.如果输入正确,则提示欢迎信息
3.如果输入账号不存在,则提示账号不存在
4.如果输入账号与密码不匹配,提示验证失败
5.同一账号,如果连续输入三次错误密码,则锁定该账号2min,2min后重新输入,如果账号密码匹配,则登录成功
6.如果三次登录内成功登录,则清空登录错误次数
7.登录时,如果账号已被锁定,提示账号已锁定信息
"""
流程图二
实例代码二
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 需求点: 1.用户输入账号与密码 2.如果输入正确,则提示欢迎信息 3.如果输入账号不存在,则提示账号不存在 4.如果输入账号与密码不匹配,提示验证失败 5.同一账号,如果连续输入三次错误密码,则锁定该账号2min,2min后重新输入,如果账号密码匹配,则登录成功 6.如果三次登录内成功登录,则清空登录错误次数 7.登录时,如果账号已被锁定,提示账号已锁定信息 user.txt: Milton Loveyp 0 Cherish Loveypgfc 0 yangp Loveypgfc 0 """ import time def modify_user(name, mode="add"): """ 修改用户名单中的登录错误次数 :param name: 用户名称 :param mode:add,登录错误次数自增1;del,登录错误次数重置为0 :return: """ # 读取用户文件 with open("user.txt", "r") as read_file: read_data = read_file.readlines() # 写入用户文件 with open("user.txt", "w") as write_file: for line in read_data: new_line = line.split() if new_line[0] == name: if mode == "add": new_line[2] = bytes(int(new_line[2]) + 1) if len(new_line) < 4: new_line.append(str(time.time())) else: new_line[3] = str(time.time()) else: new_line[2] = "0" new_line = new_line[0:3] new_line = " ".join(new_line) + "\n" write_file.write(new_line) write_file.flush() else: write_file.write(line) def auth(name, passwd, lock_time): """ 用户鉴权 :param name: 登录用户名 :param passwd: 登录密码 :return: """ with open("user.txt", "r") as read_file: for eachLine in read_file: user_list = eachLine.split() if len(user_list) < 4: user_list.append(str(time.time() - 100000)) user_name, password, errors, last_time = user_list if name == user_name: if password == passwd: if time.time() - float(last_time) > lock_time or int(errors) < 3: print "Dear [%s],welcome~" % name modify_user(name, "del") return True else: print "your accout is locked,please try after [%f]s !" % ( lock_time - (time.time() - float(last_time))) return False else: if int(errors) < 3: print "auth fail,please check your account and password!" modify_user(name, "add") return False elif time.time() - float(last_time) > lock_time: print "auth fail,please check your account and password!" modify_user(name, "del") modify_user(name, "add") return False else: print "your accout is locked,please try after [%f]s !" % ( lock_time - (time.time() - float(last_time))) return False else: print "your account is not exist!" return False def login(): """ 登录 :return: """ while True: user_name = raw_input("Name:") pwd = raw_input("PASS:") if auth(user_name, pwd, 120): break if __name__ == '__main__': login()