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

代碼實現(xiàn):
#-*- coding=utf-8 -*-
import os,sys,getpass
'''
user.txt 格式
賬號 密碼 是否鎖定 錯誤次數(shù)
jack 123 unlock 0
tom 123 unlock 0
lily 123 unlock 0
hanmeimei 123 unlock 0
lucy 123 unlock 0
'''
# 定義寫入文件的函數(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()
# 判斷用戶文件是否存在,不存在直接退出
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:
# 如果密碼錯誤則修改密碼錯誤次數(shù)
users_dic[input_name][2] = str(int(users_dic[input_name][2])+1)
# 密碼錯誤次數(shù)大于3的時候則鎖定,并修改狀態(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實現(xiàn)簡單登陸流程的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
tensorflow模型的save與restore,及checkpoint中讀取變量方式
這篇文章主要介紹了tensorflow模型的save與restore,及checkpoint中讀取變量方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
關于pytorch中全連接神經網(wǎng)絡搭建兩種模式詳解
今天小編就為大家分享一篇關于pytorch中全連接神經網(wǎng)絡搭建兩種模式詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
python 處理telnet返回的More,以及get想要的那個參數(shù)方法
今天小編就為大家分享一篇python 處理telnet返回的More,以及get想要的那個參數(shù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02
Python實現(xiàn)批量合并多個txt文件并生成Excel文件
在數(shù)據(jù)處理中,有時會面臨合并多個文本文件的任務,本文將詳細介紹如何使用Python批量合并多個txt文件,并將其生成為一個Excel文件,需要的可以參考下2023-12-12
Python中處理字符串之endswith()方法的使用簡介
這篇文章主要介紹了Python中處理字符串之endswith()方法的使用,是Python入門中的基礎知識,需要的朋友可以參考下2015-05-05
python獲得linux下所有掛載點(mount points)的方法
這篇文章主要介紹了python獲得linux下所有掛載點(mount points)的方法,涉及Python操作Linux下掛載點的相關技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04

