我是Python的新手,正在尝试使用伪造的OTP系统和登录限制器创建简单的登录。 OTP有效,但登录限制器的计数器无效。仅一次尝试失败(我想要3次)后,它就会给我所需的结果。尝试失败后的输出为:
访问已锁定。您不应该看到这里的内容。再见。
错误的用户名或密码。请重试。
错误的用户名或密码。请重试。
错误的用户名或密码。请重试。
以退出代码1完成的过程
代码如下:
def old_acc():
count = 0 # count created to limit number of failed logins
login = input("Username: ") # prompts user to login in with their username
pw = input("Password: ") # prompts user to login in with their password
while count <= 3:
for line in open("db.txt", "r").readlines():
acc_info = line.split()
# if username and pw do not match, prompt user to try again
if login != acc_info[0] and pw != acc_info[1]:
print("\nIncorrect Username or Password. Please try again.\n")
count += 1
# if username and pw match, login is successful; generate otp
else:
gen_otp()
print("ACCESS GRANTED")
access_info()
# if failure count is = 3, deny access and lock out.
if count == 3:
# stops code and doesn't allow any further input.
sys.exit("ACCESS LOCKED. YOU DON'T DESERVE TO SEE WHAT'S HERE. GOODBYE.")
以下是用于生成OTP的代码,供参考。
def gen_otp():
digits = "0123456789" # digits for OTP generation
otp = ""
for i in range(4):
otp += digits[math.floor(random.random() * 10)]
mbox("Enter OTP", otp, 1) # gives user message with OTP
otp_input = input("Enter OTP: ")
if otp == otp_input:
print("ACCESS GRANTED")
access_info()
return otp
谢谢。
答案 0 :(得分:1)
您的for循环检查db.txt
文件中的每一行,如果与密码不匹配,则使计数器递增。假设db.txt
可能包含多个密码,则该计数器在第一次尝试中将已经达到4。仅当db.txt的NO行与密码匹配时,才想增加计数器。
def old_acc():
count = 0 # count created to limit number of failed logins
success = False # keeps track of succesful login
while count <= 3 and not success:
login = input("Username: ") # prompts user to login in with their username
pw = input("Password: ") # prompts user to login in with their password
for line in open("db.txt", "r").readlines():
acc_info = line.split()
# if username and pw match, login is successful; generate otp
if login == acc_info[0] and pw == acc_info[1]:
gen_otp()
print("ACCESS GRANTED")
access_info()
success = True
break
# if username and pw do not match, prompt user to try again
if not success:
print("\nIncorrect Username or Password. Please try again.\n")
count += 1
# if failure count is = 3, deny access and lock out.
if count == 3:
# stops code and doesn't allow any further input
sys.exit("ACCESS LOCKED. YOU DON'T DESERVE TO SEE WHAT'S HERE. GOODBYE.")