Python實(shí)現(xiàn)簡單登錄驗(yàn)證
本文實(shí)例為大家分享了簡單的Python登錄驗(yàn)證,供大家參考,具體內(nèi)容如下
編寫登錄接口
要求:1、輸入用戶名密碼
2、認(rèn)證成功后顯示歡迎信息
3、輸錯(cuò)三次后鎖定
#coding=utf-8 __author__ = 'wangwc' import sys,os count = 0 locked = 0 mark_user = 0 mark_passwd = 0 #獲取路徑 def cur_file_dir(): path = sys.path[0] if os.path.isdir(path): return path elif os.path.isfile(path): return os.path.dirname(path) #print (cur_file_dir()) path = cur_file_dir() #print(path) path1 = path.replace("\\",'/') + '/' #print (path1) #path2 = path1 + '/' #循環(huán)輸入 while count < 3: name = input("Username:").strip() if len(name) == 0: print ("Username can not be empty....") continue key = input("Password:").strip() if len(key) == 0: print("The password can not be empty!Try again...") continue f = open(path1 + "username.txt","r") userlist = f.readlines() for user in userlist: if user.strip() == name: mark_user = 1 f.close() if mark_user == 1: f = open(path1 + "%s_lock.txt" %(name),"r") locked = int(f.readline().strip()) f.close() else: print ("Username or Passsord wrong....") break if locked == 1: print("Sorry, the username had been locked!!!Please call the system administrator...") else: f = open (path1 + "%s_passwd.txt" %(name),"r") passwd = (f.readline().strip()) if passwd.strip() == key: mark_passwd = 1 if mark_user == 1 and mark_passwd == 1: f = open("%s_count.txt" %(name),"w") f.write("0") f.close() print("%s,welcome BABY!" %(name) ) #input('Press Enter to exit') else: f = open("%s_count.txt" %(name),"r") count = int((f.read().strip())) f.close() count +=1 f = open("%s_count.txt" %(name),"w") f.write(str(count)) f.close() print ("Username or password wrong!And the username '%s' has %d more chances to retry!" %(name,3 - count)) if(count == 3): print ("'%s' has been locked!!!" %(name)) if os.path.exists(path1 + "%s_lock.txt" %(name)): fobj = open(path1 + "%s_lock.txt" %(name),"w") fobj.writelines("1\n") else: print ("Username or password wrong!") continue
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Python greenlet和gevent使用代碼示例解析
這篇文章主要介紹了Python greenlet和gevent使用代碼示例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04python跨文件夾調(diào)用別的文件夾下py文件或參數(shù)方式詳解
這篇文章主要給大家介紹了關(guān)于python跨文件夾調(diào)用別的文件夾下py文件或參數(shù)方式的相關(guān)資料,在python中有時(shí)候我們需要調(diào)用另一.py文件中的方法或者類,需要的朋友可以參考下2023-08-08http通過StreamingHttpResponse完成連續(xù)的數(shù)據(jù)傳輸長鏈接方式
這篇文章主要介紹了http通過StreamingHttpResponse完成連續(xù)的數(shù)據(jù)傳輸長鏈接方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Python中Scrapy+adbapi提高數(shù)據(jù)庫寫入效率實(shí)現(xiàn)
本文主要介紹了Python中Scrapy+adbapi提高數(shù)據(jù)庫寫入效率實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10python自動發(fā)微信監(jiān)控報(bào)警
這篇文章主要為大家詳細(xì)介紹了python自動發(fā)微信監(jiān)控報(bào)警,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Python技巧匿名函數(shù)、回調(diào)函數(shù)和高階函數(shù)
本文分享的是Python技巧匿名函數(shù)、回調(diào)函數(shù)和高階函數(shù),我們在Python中使用lambda表達(dá)式來使用匿名函數(shù),回調(diào)函數(shù)即callback,先寫一個(gè)函數(shù),讓預(yù)先寫好的系統(tǒng)來調(diào)用,一個(gè)函數(shù)可以作為參數(shù)傳給另外一個(gè)函數(shù),或者一個(gè)函數(shù)的返回值為另外一個(gè)函數(shù),滿足其一則為高階函數(shù)2021-12-12