zoukankan      html  css  js  c++  java
  • 案例:Python用户登录以及锁定接口 Jil

    文件处理(调用文件中的值,此处为等待时间值)

    使用OS模块,与操作系统交互

    #此处写锁定
    #判断某个文件是否存在
    #if os.path.exists("文件存放路径")
    if os.path.exists("locked"):
        print("文件存在")
        #此处将文件中的锁定时间读取出来,并实现锁定秒数
        #文件处理
        #with open("文件名","打开的模式","字符编码")
        #r : read 读取
        with open("locked","r",encoding ="utf-8") as f:
            wait_time = f.read()
            print("锁定时间为",wait_time,"s")
            print("类型为",type(wait_time))
            #将str转换为int类型   int(变量)
            #若文件中存放时间,则从文件中读取等待的时间
            time.sleep(int(wait_time))
            print("解锁成功,请继续使用")
    else:
        print("文件不存在")

    注意:需在python文件路径下创建一个locked.txt文件。或者调用os.path.exists()时直接填写路径

    文件操作:写入一个text文件,默认写入当前程序所在文件夹

    with open("%s.text"%username,"w",encoding='utf-8')as f:
        f.write("当前用户%s已被锁定"% (username))

    文件操作:移除text文件

    os.remove("%s.text"%username)

    直接使用time模块

    if number == 4: #time
        import time     #time是python解释器自带的一个模块,可以通过“模块名.模块功能”使用,import添加模块
        # time.sleep()  等待的时间必须是整型
    
        print("账户锁定,等待3s后解锁")
        time.sleep(3)
        print("账号已解锁,请继续使用")
        number = 1
    # 选做题:编写用户登录接口
    #1、输入账号密码完成验证,验证通过后输出"登录成功"
    #2、可以登录不同的用户
    #3、同一账号输错三次锁定(附加功能,在程序一直运行的情况下,一旦锁定,则锁定5分钟后自动解锁)
    #扩展需求:在3的基础上,完成用户一旦锁定,无论程序是否关闭,都锁定5分钟
    import time
    import os
    
    user_data = {"FishBall":'188741', "Jil":'1118'}
    tag = True
    user_error = {"FishBall": {"cout": 0, "locked": False}, "Jil": {"cout": 0, "locked": False}}
    while tag :
        username = input("请输入您的用户名:")
        if username == 'Q':
            break
        else:
            # 在此处判断当前用户是否被锁定,若被锁定,则等待5分钟
            # os.path.exists('文件的路径')  判断文件是否存在,存在则返回True,否则返回False
            if user_error[username]['locked'] or os.path.exists("%s.text"%username):
                print("当前用户需要锁定5秒")
                time.sleep(5)
                print("解锁成功")
                user_error[username]['locked'] = False
                user_error[username]['cout'] = 0
                os.remove("%s.text"%username)
                tag = False
            while True:
                password = input("请输入您的密码:")
                if username in user_data:
                    print(f"当前登录用户为:{username}") #相当于print("当前用户为:%s" % username) python3.5后的语法
    
                    if password == user_data.get(username): #.get() 单纯取值,不能进行运算
                        print("登陆成功")
                        break
                    else:
                        print("密码错误!")
                        user_error[username]['cout'] += 1
                        if user_error[username]['cout'] == 3:
                            user_error[username]['locked'] = True
                            print("用户已被锁定5秒")
                    # 注意: 若当前用户输错三次,则以当前用户作为文件的名字保存到本地,表示该用户已被锁定
                            with open("%s.text"%username,"w",encoding='utf-8')as f:
                                f.write("当前用户%s已被锁定"% (username))
                            break
                else:
                     print("用户名不存在!")
  • 相关阅读:
    Educational Codeforces Round 13
    Educational Codeforces Round 12
    vscode 修改标签栏样式为换行全部展示
    webpack uglifyjs 报错 Unexpected token name
    tsconfig.js 使用 paths 设置alias无效问题
    Webpack 报错 filename.indexOf is not a function 的问题
    'GL_EXT_shader_framebuffer_fetch' : extension is not supported
    Flutter命令突然无响应、vscode突然无法连接到IOS模拟器
    解决node fs.writeFile 生成csv 文件乱码问题
    数组map方法与如何使用ES5实现
  • 原文地址:https://www.cnblogs.com/zhubincheng/p/12332237.html
Copyright © 2011-2022 走看看