I am making a webserver+website with python and I am currently figuring out how to check if your login values are correct. Previously for testing I used the following code:
def checkUser(username, password): #<-- input values go in here
site_usernames = ["admin", "username1", "username2"]
site_passwords = ["admin", "password1", "password2"]
site_couples = {"admin":"admin", "username1":"password1", "username2":"password2"}
not_counter = 0
while True:
if username in site_usernames:
if password in site_passwords:
for key in site_couples:
if (username == key) and (password == site_couples[key]):
print("User %s logged in!" % username)
return True
else:
not_counter = not_counter + 1
if not_counter > 10:
print("User failed login with accountname: %s" % username)
return False
break
else:
continue
else:
pass_exists = False
break
else:
user_exists = False
break
As far as I have seen, You can not return two columns from a database as a dictionary. I have managed to get one column as a list, and I am planning to use that.
So in my new code, I have the following:
A list of usernames that are in the database
A list of encoded passwords in the database
I would like to make an authentication function that checks if:
- If the username exists in the database:
- If the password exists in the database:
- If the input username:password couple exists in all existing username:password values in the database:
- return True if all checks succeed
The problem is that I find it very difficult to manage such a thing. As you can see in my example code, I had two lists and a dict, all pre-defined. I would like to create those things on the fly, and the only one I actually need help with is how to create the username:password dictionary couples. How would I do such a thing? zip() makes tuples and not dictionaries.