Day1作业-模拟登录

实现功能如下
  • 用户输入密码,密码验证后登录成功
  • 用户登录成功后提示登录信息
  • 用户输入3次错误密码后锁定
# /usr/bin/env python
# -*- coding: utf-8 -*-
# Author:jenvid.yang
import getpass
import shutil
userspwd = {}
userssta = {}
luser = []
count = 0
usercontent = open('../config/users.inf', 'r')
for line in usercontent:
    luser.append(line.rstrip('\n'))
usercontent.close()
pwdcontent = open('../config/users.pwd', 'r')
for line in pwdcontent:
    key, value = line.rstrip('\n').split(":")
    userspwd[key] = value
pwdcontent.close()
stacontent = open('../config/users.sta', 'r')
for line in stacontent:
    key, value = line.rstrip('\n').rsplit(":")
    userssta[key] = value
stacontent.close()
while count < 3:
    username = input("pls input your name:")
    password = getpass.getpass("pls input your password:")
    judge_key = username in userssta.keys()
    if judge_key and int(userssta[username]) == 3:
        print("your passwod was locked,pls contact administartor")
        break
    elif judge_key and password == userspwd[username]:
        for i in luser:
            if username == i:
                print("welcome login system", username)
                exit(0)
    else:
        print("your username or password is invalid,pls reinput!")
    count += 1
    if count == 3 and username in userssta.keys():
        print("your password was locked,pls contact administartor")
        stacontent = open('../config/users.sta', 'r')
        new_pwd = open('../config/users_new.sta','w')
        for line in stacontent:
            if username in line:
                line = line.replace("0", "3")
            new_pwd.write(line)
        stacontent.close()
        new_pwd.close()
        shutil.move('../config/users_new.sta','../config/users.sta')
# cat users.pwd
root:redhat
admin:redhat
yzw:redhat
alex:redhat
# cat users.sta
root:0
yzw:3
alex:0
admin:0
# cat users.inf
root
admin
yzw
alex

 

posted @ 2017-11-06 11:57  Jenvid  阅读(199)  评论(0)    收藏  举报