python實(shí)現(xiàn)簡(jiǎn)單登陸流程的方法
登陸流程圖:
代碼實(shí)現(xiàn):
#-*- coding=utf-8 -*- import os,sys,getpass ''' user.txt 格式 賬號(hào) 密碼 是否鎖定 錯(cuò)誤次數(shù) jack 123 unlock 0 tom 123 unlock 0 lily 123 unlock 0 hanmeimei 123 unlock 0 lucy 123 unlock 0 ''' # 定義寫(xiě)入文件的函數(shù) def wirte_to_user_file(users,user_file_path): user_file = file(user_file_path,'w+') for k,v in users.items(): line = [] line.append(k) line.extend(v) user_file.write(' '.join(line)+'\n') user_file.close() # 判斷用戶(hù)文件是否存在,不存在直接退出 user_file_path = 'users.txt' if os.path.exists(user_file_path): user_file = file(user_file_path,'r') else: print 'user file is not exists' sys.exit(1) # 遍歷用戶(hù)文件,將用戶(hù)包裝成字典 users_dic = {} for user_line in user_file: user = user_line.strip().split() users_dic[user[0]] = user[1:] ''' { 'lucy': ['123', 'unlock', '0'], 'lily': ['123', 'unlock', '0'], 'jack': ['123', 'unlock', '0'], 'hanmeimei': ['123', 'unlock', '0'], 'tom': ['123', 'unlock', '0'] } ''' while True: # 輸入賬號(hào) input_name = raw_input('please input your username,input "quit" or "q" will be exit : ').strip() # 判斷是否為退出 if input_name == 'quit' or input_name == 'q': sys.exit(0) # 輸入密碼 password = getpass.getpass('please input your password:').strip() # 判斷賬號(hào)是否存在、是否鎖定 if input_name not in users_dic: print 'username or password is not right' break if users_dic[input_name][1] == 'lock': print 'user has been locked' break # 判斷密碼是否正確,正確,登陸成功 if str(password) == users_dic[input_name][0]: print 'login success,welcome to study system' sys.exit(0) else: # 如果密碼錯(cuò)誤則修改密碼錯(cuò)誤次數(shù) users_dic[input_name][2] = str(int(users_dic[input_name][2])+1) # 密碼錯(cuò)誤次數(shù)大于3的時(shí)候則鎖定,并修改狀態(tài) if int(users_dic[input_name][2]) >= 3: print 'password input wrong has 3 times,user will be locked,please connect administrator' users_dic[input_name][1] = 'lock' wirte_to_user_file(users_dic,user_file_path) break wirte_to_user_file(users_dic,user_file_path)
以上這篇python實(shí)現(xiàn)簡(jiǎn)單登陸流程的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
一個(gè)入門(mén)級(jí)python爬蟲(chóng)教程詳解
這篇文章主要介紹了一個(gè)入門(mén)級(jí)python爬蟲(chóng)教程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01python 實(shí)現(xiàn)兩個(gè)變量值進(jìn)行交換的n種操作
這篇文章主要介紹了python 實(shí)現(xiàn)兩個(gè)變量值進(jìn)行交換的n種操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06tensorflow模型的save與restore,及checkpoint中讀取變量方式
這篇文章主要介紹了tensorflow模型的save與restore,及checkpoint中讀取變量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05pyqt5使用按鈕進(jìn)行界面的跳轉(zhuǎn)方法
今天小編就為大家分享一篇pyqt5使用按鈕進(jìn)行界面的跳轉(zhuǎn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06關(guān)于pytorch中全連接神經(jīng)網(wǎng)絡(luò)搭建兩種模式詳解
今天小編就為大家分享一篇關(guān)于pytorch中全連接神經(jīng)網(wǎng)絡(luò)搭建兩種模式詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01python 處理telnet返回的More,以及get想要的那個(gè)參數(shù)方法
今天小編就為大家分享一篇python 處理telnet返回的More,以及get想要的那個(gè)參數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Python實(shí)現(xiàn)批量合并多個(gè)txt文件并生成Excel文件
在數(shù)據(jù)處理中,有時(shí)會(huì)面臨合并多個(gè)文本文件的任務(wù),本文將詳細(xì)介紹如何使用Python批量合并多個(gè)txt文件,并將其生成為一個(gè)Excel文件,需要的可以參考下2023-12-12Python中處理字符串之endswith()方法的使用簡(jiǎn)介
這篇文章主要介紹了Python中處理字符串之endswith()方法的使用,是Python入門(mén)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05python獲得linux下所有掛載點(diǎn)(mount points)的方法
這篇文章主要介紹了python獲得linux下所有掛載點(diǎn)(mount points)的方法,涉及Python操作Linux下掛載點(diǎn)的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04Python制作動(dòng)態(tài)字符畫(huà)的源碼
python字符畫(huà)是一個(gè)簡(jiǎn)單有趣的圖畫(huà),它一般由程序制作而成,接下來(lái)通過(guò)本文給大家分享Python制作動(dòng)態(tài)字符畫(huà)的源碼,需要的朋友可以參考下2021-08-08