#需求
Writing the Login interface
-Enter user name password
-Show welcome message after successful authentication
-three times after the wrong lock
#脚本目录
[Email protected] opt]# tree ├──account_lock.txt├──accounts.txt└──login.py0 directories, 3 files
#脚本文件
#!/usr/bin/env python#_*_ coding:utf-8 _*_import sysretry_limit = 3retry_count = 0account_file = ' accounts.txt ' lock_file = ' account_lock.txt ' While retry_ count < retry_limit: #三次验证循环 #让用户输入用户名 username = raw_input (' \033[31m Username: \ 033[0m ') lock_check = file (lock_file) for line in lock_check.readlines (): if username in line: sys.exit () # Let the user enter the password password = raw_input (' \033[ 31m password: \033[0m ') #获得account文件的用户和密码 f = file ( Account_file, ' RB ') match_flag = False for line in f.readlines (): user,passwd = line.strip (' \ n '). Split () #对用户输入的用户密码进行匹配验证 &nbSp; if username == user and password == passwd: print ' match! ', username match_flag = True break f.close () #判断有没有匹配成功, the next cycle is unsuccessful, the welcome information is entered if match_flag == False: print ' user unmatched ' retry_count += 1 else: print "welcome login wsyht lerning system!" sys.exit () Else : #三次循环完后开始锁账户 print ' your account is locked! ' f = file (lock_file, ' ab ') f.write (username) f.close ()
#查看账户文件
[email protected] opt]# cat accounts.txt wsyht wsyht
#查看锁文件
[email protected] opt]# cat account_lock.txt [[email protected] opt]#
#执行脚本不匹配显示
[[email protected] opt]# python login.py username:jenkins password:jenkinsuser unmatched username:jenkins password:je Nkinsuser unmatched Username:jenkins password:jenkinsuser unmatchedyour account is locked!
#查看锁文件
[email protected] opt]# cat account_lock.txt jenkins[[email protected] opt]#
#执行脚本匹配显示
[email protected] opt]# python login.py username:wsyht password:wsyhtmatch! Wsyhtwelcome Login Wsyht lerning System
This article is from the "Wsyht blog" blog, make sure to keep this source http://wsyht2015.blog.51cto.com/9014030/1794940
Python User password login match verification