Python練習,類比使用者登入介面

來源:互聯網
上載者:User

標籤:python   登入   營運   類比登入   練習   

實現需求如下:

  1. 同一個使用者名稱連續失敗三次則鎖定,不管使用者名稱是否在,鎖定後在下次運行指令碼登入是同樣是鎖定狀態

  2. 使用檔案儲存體使用者名稱和密碼資訊,與檔案裡面的使用者名稱密碼進行認證對比

  3. 使用者名稱不存在和密碼錯誤提示不可以相同,登入成功,帳號被禁用,密碼錯誤,使用者不存在需要有相關的提示資訊

指令碼如下:

#!/usr/bin/python#coding:utf8class Login():     def userInfo(self):        #將使用者名稱密碼資訊檔處理成一個字典        with open("userinfo.txt", "r") as file:            userDict = {}            for i in file:                userList = i.split(":")                userDict.update({userList[0]:userList[1].rstrip()})        return userDict        def lock_userInfo(self):        #將否定的使用者的檔案處理成一個列表        with open("lock_userinfo.txt", "r") as file:            userList = []            for i in file:                userList.append(i.rstrip())        return userList        def lockUser(self, username):        #如果相同使用者登入錯誤三次就調用此函數,將使用者永久瑣定,寫入檔案        with open("lock_userinfo.txt", "a") as file:            file.write(username + "\n")     def userLogin(self):        #登入        lockList = []        while True:            username = raw_input("請輸入使用者名稱: ")            password = raw_input("請輸入使用者密碼: ")            if lockList.count(username) < 3:                lock = self.lock_userInfo()                user = self.userInfo()                if username not in lock:                    if username in user:                        if user[username] == password:                            print("登入成功")                        else:                            lockList.append(username)                            print("密碼錯誤")                    else:                        lockList.append(username)                        print("用名不存在")                else:                    print("此使用者已禁用")            else:                self.lockUser(username)                print("使用者登入次數超過限制,已禁用") if __name__ == "__main__":    login = Login()    login.userLogin()

指令碼使用方法:

  1. 首先需要在指令碼所在的目錄下面建立兩個檔案lock_userinfo.txt和userinfo.txt

  2. userinfo.txt存入使用者資訊,一行一個使用者,使用者名稱和密碼用冒號分開,不要有空格,如下所示:


  3. lock_userinfo.txt為被否定的使用者列表檔案,初始為空白,如果有同一使用者登入出錯三次就會被永久寫入該檔案,無法登入,解鎖使用者就是刪除該檔案裡面的使用者名稱

運行結果如下:

  1. 正確登入


  2. 密碼輸入錯誤三次


  3. 使用者被永久否定後再登入

  4. 使用者名稱輸入錯誤多次後

    這裡是第五次才禁用,和需求有點出入,邏輯還存在一些問題,沒有想到好的方法


Python練習,類比使用者登入介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.