• 案例-登录账号


    要求

    1. 输入账号密码完成验证,验证通过后输出"登录成功"
    2. 可以登录不同的用户
    3. 同一账号输错三次锁定(附加功能,在程序一直运行的情况下,一旦锁定,则锁定5分钟后自动解锁)
    4. 扩展需求:在3的基础上,完成用户一旦锁定,无论程序是否关闭,都锁定5分钟
    import time, os, datetime
    user_pwd = {"egon":"root123", "mac":"13579", "tank":"root"}
    user_info = {"egon":0, "mac":0, "tank":0}
    
    def file(filename):
        with open(f'{filename}.txt', 'w', encoding='utf-8') as f:
            f.write("unlock " + "None " + "None")
    
    def readfile(filename):
        with open(f'{filename}.txt', 'r', encoding='utf-8') as f:
            all = f.read()
            a = all.split()
            return a
    
    def lock(name):
        user_info[name] = "lock"
        print("你的账号已被锁定,需要等待20秒")
        now_times = time.time()
        with open(name+'.txt', 'w', encoding='utf-8') as f:
            f.write("lock "+name+" "+ str(now_times))
    
    def locked(filename):
        if os.path.exists(f'{filename}.txt'):
            f,n,t=readfile(filename)
            return f,n,t
        else:
            file(filename)
            f,n,t=readfile(filename)
            return f, n, t
    
    def check(upwd,pwd):
        if upwd != pwd:
            print("用户密码错误")
            return 0
        else:
            print("登录成功")
            return 1
    
    def difftime(locktime):
        curtime = datetime.datetime.now()
        pwd_time = datetime.datetime.fromtimestamp(float(locktime))
        difftime = (curtime - pwd_time).seconds
        return difftime
    
    while True:
        userName = "".join(input("请输入用户名:").split())
        password = "".join(input("请输入用户密码:").split())
        diff_time = times = 0
        flag = name = ''
        if userName in user_pwd:
            flag, name, times = locked(userName)
        else:
            print("用户不存在")
            continue
        if flag == "lock":
            diff_time = difftime(times)
            if diff_time < 20:
                print("你的账号已被锁定,需等待%s秒" % (20-int(diff_time)))
                continue
            else:
                file(userName)
                user_info[userName] = 0
                f = check(user_pwd.get(userName), password)
                if f:
                    break
                user_info[userName] += 1
        else:
            f = check(user_pwd.get(userName), password)
            if f:
                break
            user_info[userName] += 1
            if user_info.get(userName) == 3:
                lock(userName)
    注意:为了测试,时间改为20秒
    
  • 相关阅读:
    JSONObject登录接口
    HttpClient跨域请求post
    线段树个人理解及模板
    Python基本语法(一)
    Boyer-Moore算法
    Sunday算法浅谈
    Kmp算法浅谈
    bm坏字符 , Horspool算法 以及Sunday算法的不同
    字典树的建立和基本查找
    CF Round551 Div2 题解
  • 原文地址:https://www.cnblogs.com/chenwenyin/p/12332116.html
Copyright © 2020-2023  润新知