我问过一个类似的问题,可以在这里找到:Python Login Script; Usernames and Passwords in a separate file
但是,我现在正在寻求扩展脚本,以便它可以查看文本文件中每个用户帐户的多个密码,并知道第一个密码是合法密码,而链接到该帐户的任何其他指定密码将抛出发出错误信息。
现在,外部用户名文件已设置为:
管理:AdminPW
现在我想扩展它以设置类似
的东西管理员:AdminPW; adminpw; adminpw123;联系
如果输入最后4个密码,则会抛出特定的错误消息
我的代码是:
import getpass
credentials = {} ## Sets up an array for the login credentials
with open('Usernames.txt', 'r') as f: ## Opens the file and reads it
for line in f: ## For each line
username, password = line.strip().split(':') ## Separate each line into username and password, splitting after a colon
credentials[username] = password ## Links username to password
loop = 'true'
while (loop == 'true'):
username = raw_input("Please enter your username: ") ## Asks for username
if (username in credentials): ## If the username is in the credentials array
loop1 = 'true'
while (loop1 == 'true'):
password = getpass.getpass("Please enter your password: ") ## Asks for password
if (password == credentials[username]): ## If the password is linked to the username
print "Logged in successfully as " + username ## Log in
loop = 'false'
loop1 = 'false'
else:
print "Password incorrect!"
else:
print "Username incorrect!"
我尝试过使用“.strip(';')”,但它没有用。我对Python仍然缺乏经验,所以我不确定下一步该怎么做
非常感谢任何帮助!
答案 0 :(得分:1)
import getpass
credentials = {} ## Sets up an array for the login credentials
with open('Usernames.txt', 'r') as f: ## Opens the file and reads it
for line in f: ## For each line
username, delim, password = line.strip().partition(':') ## Separate each line into username and password, splitting after a colon
credentials[username] = password.split(';') ## Links username to password
while True:
username = raw_input("Please enter your username: ") ## Asks for username
if username in credentials: ## If the username is in the credentials array
while True:
password = getpass.getpass("Please enter your password: ") ## Asks for password
if password == credentials[username][0]:
print "Logged in successfully as " + username ## Log in
break
elif password in credentials[username]: ## If the password is linked to the username
print "Specific error message " + username ## Log in
else:
print "Password incorrect!"
break
else:
print "Username incorrect!"
如果您每次只要求新鲜的用户名/密码,那就更简单了。你拥有它的方式 - 如果用户错误地输入别人的用户名,他们将永远陷入循环,除非他们能猜出密码。
while True:
username = raw_input("Please enter your username: ")
password = getpass.getpass("Please enter your password: ")
if username in credentials and password == credentials[username][0]:
print "Logged in successfully as " + username
break
elif password in credentials.get(username, []):
print "Specific error message " + username
else:
print "Login incorrect!"
答案 1 :(得分:1)
我有一个密码程序,我已经完成了GCSE课程的一部分。它是python 3.2.3但原理是一样的。 您想加密密码。执行此操作的最佳方法(我认为)是哈希密码并将其保存在.txt文件中。 示例...
file = open("pwdfile.txt", "a+")
pwd = getpass.getpass("Enter Password...").encode("utf-8")
pwd = hashlib.sha512(pwd).hexdigest()
file.write(pwd)
file.close()
现在您的密码已经完全加密,没有破解者会知道它是什么。 pwdfile.txt看起来像这样: " 522e8eca613e7b41251ad995ba6406571f0b62a701e029c8e1eb24cb3b93f89a95c296aa91cde7dcb8da86fda66eda5432b206a7bc3e9b74f033d961da962e1b"
现在读取密码,即。要登录,你需要输入用户密码,以相同的方式对其进行哈希处理,如果两者匹配,则将其登录。哈希的好处是,即使黑客掌握了pwdfile.txt,他们也可以&#t; t& #34;解码"就这样。
要记录某人:
file = open("pwdfile.txt", "a+")
pwd = file.read()
userpwd = getpass.getpass("Enter password...").encode("utf-8")
userpwd = hashlib.sha512(pwd).hexdigest()
if userpwd == pwd:
print("LOGIN")
else:
print("ERROR")
希望这有帮助! :d
答案 2 :(得分:0)
密码==凭证[用户名]我不明白你要用这条线做什么。凭证[username]将所有密码作为单个字符串返回。如果您正在尝试查找与输入的密码和要存储的值匹配的匹配项,请使用split(;)再次拆分并与每个结果进行比较。