文件 | 功能 |
username_count.txt | 记录用户输错密码的次数,最大为3次,如果用户密码输入正确,则重置为0,默认为0 |
username_lock.txt | 记录用户是否被锁定,1表示锁定,0表示未锁定,默认为0 |
username_passwd.txt | 记录用户的密码 |
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
import
sys,os
count =
0
#To count,
if
the user
is
locked.
mark_user =
0
#To make sure
if
the user
is
existing.
mark_passwd =
0
#To make sure
if
the password
is
right.
while
count <
3
:
name = raw_input(
'Username:'
).strip()
if
len(name) ==
0
:
print
'The username can not be empty!'
continue
key = raw_input(
'Password:'
).strip()
if
len(key) ==
0
:
print
'The password can not be empty!Try again!'
continue
#Upon:To check
if
the username or password
is
empty.检查用户名或密码是否为空
f = file(
'username.txt'
,
'r'
)
userlist = f.readlines()
f.close()
for
user
in
userlist:
if
user.strip() == name:
mark_user =
1
#检查输入的用户是否在username.txt文件中,即是否注册,如果在,mark_user =
1
if
mark_user ==
1
:
f = file(
'%s_lock.txt'
% (name),
'r'
)
locked =
int
(f.readline().strip())
f.close()
else
:
print
'Username or password wrong!'
break
#Upon:To check
if
exist,
if
not exist,printing username or password wrong!And quit the program.
#用户名不存在就提示用户名或密码错误,用户名存在就检查该用户是否被锁定
if
locked ==
1
:
sys.exit(
'Sorry, the username had been locked!!!Please call the system administrator.'
)
#Upon:To check
if
the username
is
locked by the system.
else
:
f = file(
'%s_passwd.txt'
% (name),
'r'
)
passwd = (f.readline().strip())
if
passwd.strip() == key:
mark_passwd =
1
# check the password from the name_input password file.
if
mark_user ==
1
and mark_passwd ==
1
:
f = file(
'%s_count.txt'
% name,
'w'
)
f.write(
'0'
)
f.close()
sys.exit(
'%s, welcome to our system!'
% name)
#用户名和密码正确,将username_count.txt内容重置为
0
else
:
f = file(
'%s_count.txt'
% name,
'r'
)
count =
int
((f.read().strip()))
f.close()
count +=
1
f = file(
'%s_count.txt'
% name,
'w'
)
f.write(str(count))
f.close()
#open the count file of the user and change it.
#密码不正确,就把count次数记录在该用户名对应的文件count文件上
print
''
'Username or password wrong!
And the username
'%s'
has %d more chances to retry!
''
' % (name,
3
- count)
if
count ==
3
:
print
"'%s' has been locked!!!"
% (name)
if
os.path.exists(
'%s_lock.txt'
% (name)):
fobj = open(
'%s_lock.txt'
% (name),
'w'
)
fobj.writelines(
'1\n'
)
else
:
print
'Username or password wrong!'
continue
#Upon:To check
if
the username and password are right,
if
not, the count will add
1
.
#如果count次数为
3
,则将用户对应的lock文件内容重置为
1
,锁定该用户
|
1
2
3
4
5
6
7
8
9
10
11
|
xpleaf@xpleaf-machine:~/seminar6/day1$ head username.txt xpleaf_count.txt xpleaf_lock.txt xpleaf_passwd.txt
==> username.txt <==
xpleaf
==> xpleaf_count.txt <==
0
==> xpleaf_lock.txt <==
0
==> xpleaf_passwd.txt <==
xpleaf123
|
1
2
3
4
|
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py
Username:xpleaf
Password:xpleaf123
xpleaf, welcome to our system!
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py
Username:xpleaf
Password:klkdf
Username or password wrong!
And the username
'xpleaf'
has
2
more chances to retry!
Username:xpleaf
Password:kldf
Username or password wrong!
And the username
'xpleaf'
has
1
more chances to retry!
Username:Traceback (most recent call last):
File
"newlogin.py"
, line
8
,
in
<module>
name = raw_input(
'Username:'
).strip()
EOFError
xpleaf@xpleaf-machine:~/seminar6/day1$ cat xpleaf_count.txt
2
|
1
2
3
4
5
6
|
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py
Username:xpleaf
Password:xpleaf123
xpleaf, welcome to our system!
xpleaf@xpleaf-machine:~/seminar6/day1$ cat xpleaf_count.txt
0
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
0xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py
Username:xpleaf
Password:kldf
Username or password wrong!
And the username
'xpleaf'
has
2
more chances to retry!
Username:xpleaf
Password:klkdf
Username or password wrong!
And the username
'xpleaf'
has
1
more chances to retry!
Username:xpleaf
Password:kldf
Username or password wrong!
And the username
'xpleaf'
has
0
more chances to retry!
'xpleaf'
has been locked!!!
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py
Username:xpleaf
Password:xpleaf123
Sorry, the username had been locked!!!Please call the system administrator.
xpleaf@xpleaf-machine:~/seminar6/day1$ cat xpleaf_lock.txt
1
|
1
2
3
4
|
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py
Username:klkdlf
Password:klkdf
Username or password wrong!
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
xpleaf@xpleaf-machine:~/seminar6/day1$ head username.txt yonghaoyip_count.txt yonghaoyip_lock.txt yonghaoyip_passwd.txt
==> username.txt <==
xpleaf
yonghaoyip
==> yonghaoyip_count.txt <==
0
==> yonghaoyip_lock.txt <==
0
==> yonghaoyip_passwd.txt <==
yonghaoyip123
|
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
|
对用户yonghaoyip操作:
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py
Username:yonghaoyip
Password:klkdf
Username or password wrong!
And the username
'yonghaoyip'
has
2
more chances to retry!
Username:yonghaoyip
Password:kldf
Username or password wrong!
And the username
'yonghaoyip'
has
1
more chances to retry!
Username:Traceback (most recent call last):
File
"newlogin.py"
, line
8
,
in
<module>
name = raw_input(
'Username:'
).strip()
EOFError
对用户xpleaf操作:
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py
Username:xpleaf
Password:kkjlkdf
Username or password wrong!
And the username
'xpleaf'
has
2
more chances to retry!
Username:xpleaf
Password:xpleaf123
xpleaf, welcome to our system!
查看两个用户对应的count文件记录:
xpleaf@xpleaf-machine:~/seminar6/day1$ head yonghaoyip_count.txt xpleaf_count.txt
==> yonghaoyip_count.txt <==
2
==> xpleaf_count.txt <==
0
|