如何使用外部文本文件创建身份验证
How to create authentication using an external text file
作为一个更大程序的一部分,我需要根据外部 txt 文件检查某人的用户名和密码。
我需要在我的文本文件中使用字典吗?
username = input("What is your username?")
password = input("Password?")
#To do: check username + password against external file users.txt
#users.txt is in the same directory as the program
好吧,如果您的密码没有加密,您可以直接读取文件,并将其拆分以在字典中包含不同的用户和密码。
例如,如果您的文件格式为每行一个用户和一个密码,由 space 分隔,您可以使用这样的程序:
username = "username"
password = "password"
with open("users.txt", "r") as passwordsfile:
passwordSeparator = " "
# One line format, you can use one of the following solutions
passwords = {
x.split(passwordSeparator)[0]: x.split(passwordSeparator)[
1
] # x should be in "username password" format, so we have to split it
for x in filter(
None, passwordsfile.read().split("\n")
) # The filter function remove the empty lines
}
# Multiline solution:
passwords = {}
separatedLines = filter(None, passwordsfile.read().split("\n"))
for x in separatedLines:
_username = x.split(passwordSeparator)[0]
_password = x.split(passwordSeparator)[1]
passwords[_username] = _password
# print(passwords) : {"user1": "password1", "user2": "password2"}
# Check the user and password with assert
try:
assert (
passwords[username] == password
) # assert is useful to check a condition in our program. This is useful in a try/except bracket, because in this case, we have no guarentee that the username is in the password database
print("You're logged in")
except:
print("Wrong password or username!")
但是,如果您有一个大型用户数据库,您应该查看数据库存储(SQLite、MySQL、...)以提高性能,并且您可能需要加密您的用户密码。
无论我从你的问题中得到什么,你都想
创建一个 login/Authentication 从文本文件中调用信息的程序,
def main():
username, password = get_name_and_password()
registered_users = read_pwdfile('pwd_filename')
if usr_pass_registered(username, password, registered_users):
registered = True
else:
registered = get_registration(username, password, 'pwd_filename')
if registered:
print(You are logged in)
#您需要以csv格式保存您的用户名和密码(逗号分隔)
作为一个更大程序的一部分,我需要根据外部 txt 文件检查某人的用户名和密码。
我需要在我的文本文件中使用字典吗?
username = input("What is your username?")
password = input("Password?")
#To do: check username + password against external file users.txt
#users.txt is in the same directory as the program
好吧,如果您的密码没有加密,您可以直接读取文件,并将其拆分以在字典中包含不同的用户和密码。
例如,如果您的文件格式为每行一个用户和一个密码,由 space 分隔,您可以使用这样的程序:
username = "username"
password = "password"
with open("users.txt", "r") as passwordsfile:
passwordSeparator = " "
# One line format, you can use one of the following solutions
passwords = {
x.split(passwordSeparator)[0]: x.split(passwordSeparator)[
1
] # x should be in "username password" format, so we have to split it
for x in filter(
None, passwordsfile.read().split("\n")
) # The filter function remove the empty lines
}
# Multiline solution:
passwords = {}
separatedLines = filter(None, passwordsfile.read().split("\n"))
for x in separatedLines:
_username = x.split(passwordSeparator)[0]
_password = x.split(passwordSeparator)[1]
passwords[_username] = _password
# print(passwords) : {"user1": "password1", "user2": "password2"}
# Check the user and password with assert
try:
assert (
passwords[username] == password
) # assert is useful to check a condition in our program. This is useful in a try/except bracket, because in this case, we have no guarentee that the username is in the password database
print("You're logged in")
except:
print("Wrong password or username!")
但是,如果您有一个大型用户数据库,您应该查看数据库存储(SQLite、MySQL、...)以提高性能,并且您可能需要加密您的用户密码。
无论我从你的问题中得到什么,你都想 创建一个 login/Authentication 从文本文件中调用信息的程序,
def main():
username, password = get_name_and_password()
registered_users = read_pwdfile('pwd_filename')
if usr_pass_registered(username, password, registered_users):
registered = True
else:
registered = get_registration(username, password, 'pwd_filename')
if registered:
print(You are logged in)
#您需要以csv格式保存您的用户名和密码(逗号分隔)