Python實現簡單登陸流程的方法

編程語言 Python Line 技術 扣丁學堂 2018-12-05

本篇文章小編主要和大家分享一下扣丁學堂簡述的Python實現簡單登陸流程的方法,文章中會有代碼列出供大家參考學習,對Python開發感興趣的小夥伴就隨小編一起來了解一下吧。

Python實現簡單登陸流程的方法

Python培訓

登陸流程圖:

Python實現簡單登陸流程的方法

Python學習

代碼實現:

#-*- coding=utf-8 -*-

import os,sys,getpass

'''

user.txt 格式

賬號 密碼 是否鎖定 錯誤次數

jack 123 unlock 0

tom 123 unlock 0

lily 123 unlock 0

hanmeimei 123 unlock 0

lucy 123 unlock 0

'''

# 定義寫入文件的函數

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()

# 判斷用戶文件是否存在,不存在直接退出

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)

# 遍歷用戶文件,將用戶包裝成字典

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:

# 輸入賬號

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()

# 判斷賬號是否存在、是否鎖定

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:

# 如果密碼錯誤則修改密碼錯誤次數

users_dic[input_name][2] = str(int(users_dic[input_name][2])+1)

# 密碼錯誤次數大於3的時候則鎖定,並修改狀態

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實現簡單登陸流程的方法,希望對小夥伴們有所幫助,想要了解更多內容的小夥伴可以登錄扣丁學堂官網諮詢。

相關推薦

推薦中...