簡體   English   中英

經過一定次數的嘗試后,Python密碼系統已鎖定

[英]Python password system with lock after certain number of attempts

因此,我試圖在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\n0")
            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\n")
            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("\n")
            f.close()
            break
    else:
        count += 1
        f = open("PhysxInit.txt", "w")
        f.write(("1"*count)+"\n"+str(tima))
        if count == 5:
             f.write(str(time.time()+60*5))
        f.close()

#f = open("PhysxInit.txt", "w")
#f.write("\n")
#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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM