天天看點

python基礎學習登陸作業

blog addr: http://www.cnblogs.com/uyahs/p/7667783.html
program summary:
       該程式運作在python3.X環境中
       該程式共兩個檔案,需要放在同一目錄下。
       主程式為zuoye_login.py
       zuoye_login.txt 為存儲賬号資訊的檔案,如需增加賬号密碼格式如下:
       user:password;0

       程式運作後會先進行賬号密碼的輸入,輸入後程式會從密碼文本中進行查找和比對資訊,如果正确則顯示成功資訊
       如果失敗則對該賬号記錄1次錯誤登陸,超過三次錯誤登陸,則鎖定該賬号100秒。      
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Xtian
'''
        這是一個登陸子產品,賬号密碼儲存在同目錄下zuoye_login.txt檔案下
        zoye_login.txt檔案格式如:root;server;0     分别是賬号,密碼,登陸失敗次數,三個值之間用分号分隔
        運作此程式後,如果密碼錯誤則會對該賬号記錄一次錯誤,超過三次則鎖定該賬号100秒。
'''



import  getpass
import time
login_status = 0   #這個值為登陸狀态,如果等于1表示登陸驗證成功
while True:
    f = open('zuoye_login.txt','r+')
    text = f.read().split('
')
    f.close()
    guess_user = input("user:")
    guess_passwd = getpass.getpass("password:")
    for j in range(len(text)):
        if guess_user == text[j].split(';')[0]:
            if int(text[j].split(';')[2]) > 2:          #如果該賬号密碼登陸錯誤次數超過2
                localtime = time.localtime(time.time())
                now_time = int(time.mktime(localtime))    #擷取現在的時間戳
                unit_time = int(now_time)-int(text[j].split(';')[3])    #計算出目前時間和記錄時間差
                if unit_time > 100:                                     #将時間差與100比較,超過100則可以繼續
                    text[j] = "%s;%s;0" % (text[j].split(';')[0],text[j].split(';')[1])   #将失敗次數重置為0
                    new_text = '
'.join(text)
                    f = open('zuoye_login.txt', 'w')
                    f.writelines(new_text)
                    f.close()
                else:                           #時間差不足100秒,按照登陸失敗重新循環
                    wait_time = 100 - unit_time         #目前還需要等待的時間(秒)
                    print("There are too many errors,please wait for %s second" % (wait_time))
                    input("please input 'ENTER' to try again")
                    break
            if guess_passwd == text[j].split(';')[1]:     #判斷密碼是否正确
                login_status = 1                     #将登陸狀态更改為1,表示登陸成功
                break
            else:
                if text[j].split(';')[2]:                #判斷zuoye_login.txt檔案中這個賬号中的失敗次數不為空時進行
                    failed_count = int(text[j].split(';')[2])       #擷取該賬号失敗次數
                    failed_count += 1                                  #将該賬号失敗次數+1
                    localtime = time.localtime(time.time())
                    failed_login_time = int(time.mktime(localtime))      #擷取登陸失敗時的時間戳,并存入檔案
                    text[j] = "%s;%s;%s;%d" % (text[j].split(';')[0],text[j].split(';')[1],failed_count,failed_login_time)
                    new_text = '
'.join(text)
                    f = open('zuoye_login.txt','w')
                    f.writelines(new_text)
                    f.close()
                else:                          #如果zuoye_login.txt檔案中此賬号的失敗次數為空則添加一個失敗次數的值
                    text[j] = "%s;%s;%s;%s" % (text[j].split(';')[0],text[j].split(';')[1],failed_count,failed_login_time)
                    new_text = '
'.join(text)
                    f = open('zuoye_login.txt', 'w')
                    f.writelines(new_text)
                    f.close()
        else:
            continue

    if login_status == 1:           #判斷是否登陸成功
        print("login success !")
        print("welcome ", guess_user)
        break
    else:
        print("login failed !")