1.基本需求
編寫登陸接口,實作如下需求:
(1)輸入使用者名密碼
(2)認證成功後顯示歡迎資訊
(3)輸錯三次後鎖定
2.實作細節
·每添加一個使用者,需要手動添加三個檔案
檔案
功能
username_count.txt
記錄使用者輸錯密碼的次數,最大為3次,如果使用者密碼輸入正确,則重置為0,預設為0
username_lock.txt
記錄使用者是否被鎖定,1表示鎖定,0表示未鎖定,預設為0
username_passwd.txt
記錄使用者的密碼
·注:username是指該使用者的使用者名,視具體的使用者名而定;
·每添加一個使用者,在username.txt中寫入該使用者名,一個使用者名占一行(手動);
·如果輸入的使用者名或密碼為空,都要求重新輸入;
·輸入不存在的使用者名,隻會提示“username or password wrong”;
·輸入存在的使用者名,才會進行密碼輸入錯誤次數的記錄。
3.實作代碼與注釋
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
<code>import</code> <code>sys,os</code>
<code>count = </code><code>0</code> <code>#to count, </code><code>if</code> <code>the user </code><code>is</code> <code>locked.</code>
<code>mark_user = </code><code>0</code> <code>#to make sure </code><code>if</code> <code>the user </code><code>is</code> <code>existing.</code>
<code>mark_passwd = </code><code>0</code> <code>#to make sure </code><code>if</code> <code>the password </code><code>is</code> <code>right.</code>
<code>while</code> <code>count < </code><code>3</code><code>:</code>
<code> </code><code>name = raw_input(</code><code>'username:'</code><code>).strip()</code>
<code> </code><code>if</code> <code>len(name) == </code><code>0</code><code>:</code>
<code> </code><code>print </code><code>'the username can not be empty!'</code>
<code> </code><code>continue</code>
<code> </code>
<code> </code><code>key = raw_input(</code><code>'password:'</code><code>).strip() </code>
<code> </code><code>if</code> <code>len(key) == </code><code>0</code><code>:</code>
<code> </code><code>print </code><code>'the password can not be empty!try again!'</code>
<code>#upon:to check </code><code>if</code> <code>the username or password </code><code>is</code> <code>empty.檢查使用者名或密碼是否為空</code>
<code> </code><code>f = file(</code><code>'username.txt'</code><code>, </code><code>'r'</code><code>)</code>
<code> </code><code>userlist = f.readlines()</code>
<code> </code><code>f.close()</code>
<code> </code><code>for</code> <code>user </code><code>in</code> <code>userlist:</code>
<code> </code><code>if</code> <code>user.strip() == name:</code>
<code> </code><code>mark_user = </code><code>1</code>
<code>#檢查輸入的使用者是否在username.txt檔案中,即是否注冊,如果在,mark_user = </code><code>1</code>
<code> </code><code>if</code> <code>mark_user == </code><code>1</code><code>: </code>
<code> </code><code>f = file(</code><code>'%s_lock.txt'</code> <code>% (name), </code><code>'r'</code><code>)</code>
<code> </code><code>locked = </code><code>int</code><code>(f.readline().strip())</code>
<code> </code><code>f.close() </code>
<code> </code><code>else</code><code>:</code>
<code> </code><code>print </code><code>'username or password wrong!'</code>
<code> </code><code>break</code>
<code>#upon:to check </code><code>if</code> <code>exist,</code><code>if</code> <code>not exist,printing username or password wrong!and quit the program.</code>
<code>#使用者名不存在就提示使用者名或密碼錯誤,使用者名存在就檢查該使用者是否被鎖定</code>
<code> </code><code>if</code> <code>locked == </code><code>1</code><code>:</code>
<code> </code>
<code> </code><code>sys.exit(</code><code>'sorry, the username had been locked!!!please call the system administrator.'</code><code>)</code>
<code>#upon:to check </code><code>if</code> <code>the username </code><code>is</code> <code>locked by the system.</code>
<code> </code><code>f = file(</code><code>'%s_passwd.txt'</code> <code>% (name), </code><code>'r'</code><code>)</code>
<code> </code><code>passwd = (f.readline().strip())</code>
<code> </code><code>if</code> <code>passwd.strip() == key:</code>
<code> </code><code>mark_passwd = </code><code>1</code>
<code> </code><code># check the password from the name_input password file.</code>
<code> </code><code>if</code> <code>mark_user == </code><code>1</code> <code>and mark_passwd == </code><code>1</code><code>:</code>
<code> </code><code>f = file(</code><code>'%s_count.txt'</code> <code>% name, </code><code>'w'</code><code>)</code>
<code> </code><code>f.write(</code><code>'0'</code><code>)</code>
<code> </code><code>f.close()</code>
<code> </code><code>sys.exit(</code><code>'%s, welcome to our system!'</code> <code>% name)</code>
<code>#使用者名和密碼正确,将username_count.txt内容重置為</code><code>0</code>
<code> </code>
<code> </code><code>else</code><code>:</code>
<code> </code><code>f = file(</code><code>'%s_count.txt'</code> <code>% name, </code><code>'r'</code><code>)</code>
<code> </code><code>count = </code><code>int</code><code>((f.read().strip()))</code>
<code> </code><code>count += </code><code>1</code>
<code> </code><code>f.write(str(count))</code>
<code>#open the count file of the user and change it.</code>
<code>#密碼不正确,就把count次數記錄在該使用者名對應的檔案count檔案上</code>
<code> </code>
<code> </code><code>print </code><code>''</code><code>'username or password wrong!</code>
<code>and the username </code><code>'%s'</code> <code>has %d more chances to retry!</code><code>''</code><code>' % (name, </code><code>3</code> <code>- count)</code>
<code> </code><code>if</code> <code>count == </code><code>3</code><code>:</code>
<code> </code><code>print </code><code>"'%s' has been locked!!!"</code> <code>% (name)</code>
<code> </code><code>if</code> <code>os.path.exists(</code><code>'%s_lock.txt'</code> <code>% (name)):</code>
<code> </code><code>fobj = open(</code><code>'%s_lock.txt'</code> <code>% (name), </code><code>'w'</code><code>)</code>
<code> </code><code>fobj.writelines(</code><code>'1\n'</code><code>)</code>
<code> </code><code>else</code><code>:</code>
<code> </code><code>print </code><code>'username or password wrong!'</code>
<code> </code><code>continue</code>
<code>#upon:to check </code><code>if</code> <code>the username and password are right, </code><code>if</code> <code>not, the count will add </code><code>1</code><code>.</code>
<code>#如果count次數為</code><code>3</code><code>,則将使用者對應的lock檔案内容重置為</code><code>1</code><code>,鎖定該使用者</code>
4.測試
--對單一使用者xpleaf的測試
·在對應4個檔案中添加相應資訊:
<code>xpleaf@xpleaf-machine:~/seminar6/day1$ head username.txt xpleaf_count.txt xpleaf_lock.txt xpleaf_passwd.txt </code>
<code>==> username.txt <==</code>
<code>xpleaf</code>
<code>==> xpleaf_count.txt <==</code>
<code>0</code>
<code>==> xpleaf_lock.txt <==</code>
<code>==> xpleaf_passwd.txt <==</code>
<code>xpleaf123</code>
·正常輸入使用者名和密碼:
<code>xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py </code>
<code>username:xpleaf</code>
<code>password:xpleaf123</code>
<code>xpleaf, welcome to our system!</code>
·故意輸錯兩次密碼後檢視相關檔案:
<code>password:klkdf</code>
<code>username or password wrong!</code>
<code>and the username </code><code>'xpleaf'</code> <code>has </code><code>2</code> <code>more chances to retry!</code>
<code>password:kldf</code>
<code>and the username </code><code>'xpleaf'</code> <code>has </code><code>1</code> <code>more chances to retry!</code>
<code>username:traceback (most recent call last):</code>
<code> </code><code>file </code><code>"newlogin.py"</code><code>, line </code><code>8</code><code>, </code><code>in</code> <code><module></code>
<code> </code><code>name = raw_input(</code><code>'username:'</code><code>).strip()</code>
<code>eoferror</code>
<code>xpleaf@xpleaf-machine:~/seminar6/day1$ cat xpleaf_count.txt </code>
<code>2</code>
·可以看到count檔案已經記錄為2,再輸入正确的使用者名和密碼:
·count檔案重置為0;
·連續輸入密碼錯誤超過3次:
<code>0xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py </code>
<code>and the username </code><code>'xpleaf'</code> <code>has </code><code>0</code> <code>more chances to retry!</code>
<code>'xpleaf'</code> <code>has been locked!!!</code>
<code>sorry, the username had been locked!!!please call the system administrator.</code>
<code>xpleaf@xpleaf-machine:~/seminar6/day1$ cat xpleaf_lock.txt </code>
<code>1</code>
·使用者已經被鎖定,對應lock檔案内容變為1,如果需要解鎖,則要手動将該檔案重置為0;
·輸入錯誤的使用者名:
<code>username:klkdlf</code>
·隻會提示使用者名或密碼錯誤,不會做相關檔案記錄;
--對兩個使用者xpleaf和yonghaoyip的測試
·為使用者yonghaoyip添加相關檔案資訊:
<code>xpleaf@xpleaf-machine:~/seminar6/day1$ head username.txt yonghaoyip_count.txt yonghaoyip_lock.txt yonghaoyip_passwd.txt </code>
<code>yonghaoyip</code>
<code>==> yonghaoyip_count.txt <==</code>
<code>==> yonghaoyip_lock.txt <==</code>
<code>==> yonghaoyip_passwd.txt <==</code>
<code>yonghaoyip123</code>
·主要測試兩個使用者的count檔案記錄:
<code>對使用者yonghaoyip操作:</code>
<code>username:yonghaoyip</code>
<code>and the username </code><code>'yonghaoyip'</code> <code>has </code><code>2</code> <code>more chances to retry!</code>
<code>and the username </code><code>'yonghaoyip'</code> <code>has </code><code>1</code> <code>more chances to retry!</code>
<code>對使用者xpleaf操作:</code>
<code>password:kkjlkdf</code>
<code>檢視兩個使用者對應的count檔案記錄:</code>
<code>xpleaf@xpleaf-machine:~/seminar6/day1$ head yonghaoyip_count.txt xpleaf_count.txt </code>
·可以看到兩個使用者的相關檔案并不會互相影響。