欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)簡(jiǎn)單登陸流程的方法

 更新時(shí)間:2018年04月22日 09:07:09   作者:鄭子明  
下面小編就為大家分享一篇python實(shí)現(xiàn)簡(jiǎn)單登陸流程的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

登陸流程圖:

代碼實(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)文章

最新評(píng)論