Python用户密码登陆匹配验证
#需求编写登陆接口
- 输入用户名密码
- 输错三次后锁定账户
- 认证成功后显示欢迎信息
#脚本目录
1
2
3
4
5
6
# tree
.
├── account_lock.txt
├── accounts.txt
└── login.py
0 directories, 3 files
#脚本文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/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()
#查看账户文件
1
2
# cat accounts.txt
wsyht wsyht
#查看锁文件
1
2
# cat account_lock.txt
#
#执行脚本不匹配显示
1
2
3
4
5
6
7
8
9
10
11
# 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!
#查看锁文件
1
2
# cat account_lock.txt
jenkins#
#执行脚本匹配显示
1
2
3
4
5
# python login.py
Username: wsyht
Password: wsyht
Match! wsyht
Welcome login wsyht Lerning system
页:
[1]