标签:密码 for 输出 arch http .com and mat 红色
编写可供用户查询的员工信息表(二)
要求:
1.需要用户认证
2.员工信息表文件内容:
ID Name Department Phone
3.认证成功后查询正确信息
上一篇内容:https://www.cnblogs.com/easypython/p/9080561.html
〇上一篇已经实现基本要求,但是代码还是有所欠缺,比如登录成功后输入回车或空格后把文件信息一次性读取出来了,所以此篇内容为对上一篇的修改补充;实现功能:
〇1.编写登录接口,输入用户名和密码
〇2.用户验证成功登录后显示登录欢迎信息
〇3.用户输错密码超限后锁定
〇4 不同的错误锁定方式可以有所区别
〇解决一次性输出文件所有信息的BUG
代码分享如下:
1 #!/usr/bin/env/python 2 # -*- coding: utf-8 -*- 3 import sys 4 import time 5 6 dic_account = {} 7 normoal_file = "G:\python\exsise\\file\whitelist.txt" 8 lock_file = "G:\python\exsise\\file\lock.txt" 9 with open(normoal_file) as norm_f: 10 for line in norm_f.readlines(): 11 usr,pawd = line.strip().split() 12 dic_usr_pawd = {usr:pawd} 13 dic_account.update(dic_usr_pawd) 14 15 def deny_accout(usrname): 16 print(‘\033[1;31;40m‘) # 下一目标输出背景为黑色,颜色红色高亮显示 17 print(‘*‘ * 50) 18 print(‘\033[7;31m错误次数超限,用户已被永久锁定,请联系管理员!\033[1;31;40m‘) # 字体颜色红色反白处理 19 print(‘*‘ * 50) 20 print(‘\033[0m‘) 21 with open(lock_file,‘a‘) as deny_f: 22 deny_f.write(‘\n‘) 23 deny_f.write(usrname) 24 25 def main(): 26 NumOfInput = 0 27 SetTo = ‘whole‘ 28 while SetTo == ‘whole‘: 29 usrname = input(‘\033[1;32m请输入您的用户名:\033[0m‘) 30 if list(dic_account.keys()).count(usrname) == 0: 31 if len(usrname.strip()) == 0: 32 print(‘\033[1;31m用户名不能为空,请重新输入‘) 33 continue 34 else: 35 with open(lock_file) as lock_f: 36 for line in lock_f.readlines(): 37 if usrname == line.strip(): 38 sys.exit(‘\033[1;32m用户%s已锁定,请联系管理员。\033[0m‘ % usrname) 39 usr_list = [] 40 u_list = usr_list.append(usrname) 41 redo_num = usr_list.count(usrname) 42 if NumOfInput < 5: 43 NumOfInput += 1 44 if redo_num < 3: 45 print("\033[1;31m出错了,用户名:%s没有找到,请重新输入:" % usrname) 46 else: 47 deny_accout(usrname) 48 else: 49 print(‘\033[1;33m用户名错误次数超限,请5分钟后再试‘) 50 time.sleep(300) 51 else: 52 NumOfInput = 0 53 while NumOfInput < 3: 54 passwd = input(‘\033[1;32m请输入用户%s密码:\033[0m‘ % usrname) 55 if passwd == str(dic_account[usrname]): 56 print(‘\033[1;36m登陆成功。您的所有操作有可能会被记录!‘) 57 SetTo = ‘q‘ 58 break 59 if len(passwd.strip()) == 0: 60 print(‘\033[1;33m密码不能为空,请重新输入,您还有%d次机会。‘% (2-NumOfInput)) 61 NumOfInput += 1 62 else: 63 print(‘\033[1;33m密码错误,请重新输入,您还有%d次机会。‘% (2-NumOfInput)) 64 NumOfInput += 1 65 else: 66 print(‘\033[1;31m输入次数超限,请2小时后再试‘) 67 time.sleep(7200) 68 if SetTo == ‘q‘: 69 # print("\033[7;47mEnter ‘q‘: quit ‘b‘:back " ) 70 while True: 71 match_yes = 0 72 InfoOfEmTab_file = open("TheInfoOfEmployeeTable.txt") 73 sch_input = input("\033[1;34;42mPlease enter what the information you need to search: ") 74 while True: 75 line = InfoOfEmTab_file.readline() 76 if len(line) == 0: 77 break 78 if sch_input.strip() in line: 79 if sch_input.strip() == ‘‘: 80 # print("\033[1;31mThere was no character input, please check if the input was corrected!\n ") 81 match_yes = 1 82 else: 83 print("\033[1;31mMarch Item: \033[1;36m%s" % line) 84 match_yes = 2 85 if match_yes == 0: 86 print("\033[1;31mNo match items had found!Please check it and try again.\n") 87 if match_yes == 1: 88 print("\033[1;31mThere was no character input, please check if the input was corrected!\n ") 89 if __name__ == ‘__main__‘: 90 main()
效果图:
标签:密码 for 输出 arch http .com and mat 红色
原文地址:https://www.cnblogs.com/easypython/p/9080972.html