多用户登录验证
Posted PAYNE1Z
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多用户登录验证相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python3 # -*- coding:utf-8 -*- # # Author: Payne Zheng <[email protected]> # Date: 2018-04-11 10:41:15 # Location: DongGuang # Desc: User login authentication # def CheckUserStatus(user): """检查帐号是否在锁定文件中""" try: with open(user_lock_file, ‘r‘, encoding="utf-8") as f1: lines = f1.readlines() for line in lines: if line.strip() == user: return "lock" except FileNotFoundError: pass """设定变量""" user_lock_file = "lock.txt" retry_num = 3 user_pass_dict = { "jack": "abc123", "jushua": "123abc", "payne": "a1b2c3" } input_user = input("\033[34mPlease enter your account number:\033[0m") """检查用户帐号状态,如在锁定帐号文件则报错退出""" if CheckUserStatus(input_user) == "lock": print( """\033[31mError! %s user has been locked,""" """unable to login, please contact customer service phone 10086.\033[0m""") exit() """检查用户是否存在,不存在直接退出""" if input_user not in user_pass_dict: print("\033[31mSrror! you enterd user <%s> does not exist.!\033[0m" % input_user) exit() """验证密码""" while True: input_pass = input("\033[34mPlease enter your password:\033[0m") # 检查用户输入密码是否正确 if user_pass_dict.get(input_user) == input_pass: print("\033[32mSuccessful login, welcome <%s>\033[0m" % input_user) break else: print( """\033[33mThe password entered is not correct,""" """please re-enter (you still have %s retry opportunity)!\033[0m""" % (retry_num - 1)) retry_num -= 1 if retry_num == 0: print( """\033[31mSorry, the password input error exceeds the number of retries""" """the account has been locked!\033[0m""") # 重输密码三次后将帐号存入锁定文件进行锁定 with open(user_lock_file, ‘a‘, encoding="utf-8") as f: f.writelines(input_user + "\n") break else: continue
作业需求:
基础需求: 让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后退出程序 升级需求: 可以支持多个用户登录 (提示,通过列表存多个账户信息) 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
以上是关于多用户登录验证的主要内容,如果未能解决你的问题,请参考以下文章