#需求

编写登陆接口

  - 输入用户名密码

  - 输错三次后锁定账户

  - 认证成功后显示欢迎信息

#脚本目录

[root@localhost 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()                                #让用户输入密码           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()                                #对用户输入的用户密码进行匹配验证                if username == user and password == passwd:                        print 'Match!', username                        match_flag = True                        break        f.close()                #判断有没有匹配成功,不成功则进行下一次循环,成功则输入欢迎信息        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()

#查看账户文件

[root@localhost opt]# cat accounts.txt wsyht wsyht

#查看锁文件

[root@localhost opt]# cat account_lock.txt [root@localhost opt]#

#执行脚本不匹配显示

[root@localhost opt]# python login.py  Username: jenkins Password: jenkinsUser unmatched Username: jenkins Password: jenkinsUser unmatched Username: jenkins Password: jenkinsUser unmatchedYour account is locked!

#查看锁文件

[root@localhost opt]# cat account_lock.txt jenkins[root@localhost opt]#

#执行脚本匹配显示

[root@localhost opt]# python login.py  Username: wsyht Password: wsyhtMatch! wsyhtWelcome login wsyht Lerning system