Python密码系统在经过一定次数的尝试后锁定

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python密码系统在经过一定次数的尝试后锁定相关的知识,希望对你有一定的参考价值。

所以我试图用Python创建一个密码系统,经过一定数量的不正确尝试后,用户将被阻止访问它,比如5分钟。我目前不确定如何在重新运行相同的文件后保留变量的值,然后以这种方式使用。有人可以帮我解决这个问题吗,因为我现在还不熟悉Python吗?

更新:

在用代码Jonas Wolff提供给我的实验一段时间后,我将我的代码最终确定如下:

def password(): 
    count = 0 
    currentTime = float(time.time()) 
    passwordList = ["something", "password", "qwerty", "m42?Cd", "no"] 
    passwordNum = random.randint(0, 4) 
    password = passwordList[passwordNum] 
    with open("password.txt", "r") as file:
        check = file.readline()
        initialTime = file.readline()
        if initialTime=="":
            initialTime==0
    if int(check)==1 and (currentTime-float(initialTime))<300:
        print("You are still locked")
        print("Please try again in", int(300-(currentTime-float(initialTime))), "seconds.")
        quit()
    print("The randomised password is No.", passwordNum+1) #Prints a string to let the user know which password was randomly selected
    while count<5:
        inp = input("Enter the Password: ")
        if inp==password:
            print("Access Granted")
            print()
            f = open("password.txt", "w")
            f.write("0
0")
            f.close()
            select()
        elif (count+1)==5:
            print("You have been locked")
            print("Please try again in 5 minutes")
            f = open("password.txt", "w")
            f.write("1
")
            f.write(str(currentTime))
            f.close()
            quit()
        else:
            count+=1
            print("Incorrect Password")
            print("You have", 5-count, "tries left.")
            continue

非常感谢您提供的帮助以及您回答我问题的耐心。

答案
import YourProgram # this is the program you want to run, if the program runs automaticly when opened then move the import to the part where i wrote YourProgram() and delete the YourPregram() line
import time

pswd = "something"

count = 0

with open("PhysxInit.txt","r") as file:
    file_info = file.readline()
    numa = file_info.count("1")
    count = numa


while True:
    with open("PhysxInit.txt","r") as file:
        file_info = file.readline()
        tima = file.readline()


    inp = input("What is the password:")



    if inp == pswd:

        if tima == "":
            tima = "0"  # this should solve yoúr problem with float convertion however it doesn't make sence that this step should be needed


        if str(file_info[:5]) != "11111" or time.time() > float(tima):
            YourProgram() # this is just meant as the thing you want to do when when granted acces i magined you where blocking acces to a program.
            f = open("PhysxInit.txt", "w")
            f.write("
")
            f.close()
            break
    else:
        count += 1
        f = open("PhysxInit.txt", "w")
        f.write(("1"*count)+"
"+str(tima))
        if count == 5:
             f.write(str(time.time()+60*5))
        f.close()

#f = open("PhysxInit.txt", "w")
#f.write("
")
#f.close()

这有用吗?只需确保您有一个名为PhysxInit.txt的文本文件

运行程序后,几次猜错了我的txt文件看起来像这样。

11111
1536328469.9134998

它应该看起来像我的,虽然数字可能会有所不同。

要按照您的要求阅读特定行,您需要执行以下操作:

with open("PhysxInit.txt", "r") as f:
    for w,i in enumerate(f):
        if w == #the number line you want:
            # i is now the the line you want, this saves memory space if you open a big file

以上是关于Python密码系统在经过一定次数的尝试后锁定的主要内容,如果未能解决你的问题,请参考以下文章

通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账

通过游戏学python 3.6 第一季 第七章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账

Python3 模拟用户密码输入三次错误后锁定

python用户名密码限定次数登录

为什么代码在经过一定次数的迭代后仍然无休止地运行?

初一下:Python利用while语句,设置密码次数为6次,超过6次就显示:“输入次数已达上限,被锁定”?谢谢