【Pythonの旅】第1編:ファイル処理に基づくログインインタフェース
5020 ワード
1.基本需要
ログインインタフェースを作成し、次の要件を実現します.
(1)ユーザ名パスワードの入力
(2)認証成功後のウェルカムメッセージ表示
(3)3回間違えてロック
2.実装の詳細
・ユーザーを1人追加するごとに、3つのファイルを手動で追加する必要がある
ファイル
機能
username_count.txt
ユーザーがパスワードを間違って入力した回数を最大3回記録し、ユーザーのパスワードが正しく入力された場合は0にリセットし、デフォルトは0に設定します.
username_lock.txt
ユーザーがロックされているかどうかを記録します.1はロック、0はロックされていません.デフォルトは0です.
username_passwd.txt
ユーザーのパスワードを記録
・注意:usernameとは、そのユーザのユーザ名を指し、具体的なユーザ名によって異なる.
・ユーザーを追加するたびにusername.txtにこのユーザー名を書き込み、1つのユーザー名が1行を占める(手動).
・入力したユーザー名またはパスワードが空の場合、再入力が要求される.
・存在しないユーザ名を入力すると、「username or password wrong」としか提示されない.
・存在するユーザ名を入力してこそ、パスワード入力エラー回数の記録が行われる.
3.実装コードと注釈
ログインインタフェースを作成し、次の要件を実現します.
(1)ユーザ名パスワードの入力
(2)認証成功後のウェルカムメッセージ表示
(3)3回間違えてロック
2.実装の詳細
・ユーザーを1人追加するごとに、3つのファイルを手動で追加する必要がある
ファイル
機能
username_count.txt
ユーザーがパスワードを間違って入力した回数を最大3回記録し、ユーザーのパスワードが正しく入力された場合は0にリセットし、デフォルトは0に設定します.
username_lock.txt
ユーザーがロックされているかどうかを記録します.1はロック、0はロックされていません.デフォルトは0です.
username_passwd.txt
ユーザーのパスワードを記録
・注意:usernameとは、そのユーザのユーザ名を指し、具体的なユーザ名によって異なる.
・ユーザーを追加するたびにusername.txtにこのユーザー名を書き込み、1つのユーザー名が1行を占める(手動).
・入力したユーザー名またはパスワードが空の場合、再入力が要求される.
・存在しないユーザ名を入力すると、「username or password wrong」としか提示されない.
・存在するユーザ名を入力してこそ、パスワード入力エラー回数の記録が行われる.
3.実装コードと注釈
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
4.
-- xpleaf
· 4 :
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
・ユーザー とパスワードを に する:xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py
Username:xpleaf
Password:xpleaf123
xpleaf, welcome to our system!
・パスワードを2 って した 、 を する: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
name = raw_input('Username:').strip()
EOFError
xpleaf@xpleaf-machine:~/seminar6/day1$ cat xpleaf_count.txt
2
・countファイルが2に されているのを て、 しいユーザー とパスワードを します.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
・countファイルを0にリセットする.
・パスワードの エラーが3 を えた :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
・ユーザーはすでにロックされており、 するlockファイルの は1となり、ロックを する がある は、 でファイルを0にリセットする.
・ ったユーザー を :xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py
Username:klkdlf
Password:klkdf
Username or password wrong!
・ユーザー またはパスワードのエラーのみを し、 ファイルの はしない.
--2 のユーザーに するxpleafとyonghaoyipのテスト
・ユーザーyonghaoyipに ファイル を する: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
・ に2 のユーザーのcountファイル をテストする: 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
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
・2 のユーザの ファイルが に しないことがわかる.