#需求
编写登陆接口
- 输入用户名密码
- 认证成功后显示欢迎信息
- 输错三次后锁定
#脚本目录
[root@localhost opt]# tree . ├── account_lock.txt ├── accounts.txt └── login.py 0 directories, 3 files
#脚本文件
#!/usr/bin/env python #_*_ coding:utf-8 _*_ import sys retry_limit = 3 retry_count = 0 account_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: jenkins User unmatched Username: jenkins Password: jenkins User unmatched Username: jenkins Password: jenkins User unmatched Your account is locked!
#查看锁文件
[root@localhost opt]# cat account_lock.txt jenkins[root@localhost opt]#
#执行脚本匹配显示
[root@localhost opt]# python login.py Username: wsyht Password: wsyht Match! wsyht Welcome login wsyht Lerning system
本文出自 “wsyht的博客” 博客,请务必保留此出处http://wsyht2015.blog.51cto.com/9014030/1794940
原文地址:http://wsyht2015.blog.51cto.com/9014030/1794940