0

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.

3
  • Any good reason for not to use an existing framework, like Flask or Django? Recreating a web framework for a project is not exactly the best practice - either for productivity, and more important, for security reasons. Commented Jul 28, 2013 at 20:54
  • @jsbueno Yes. We want to make it ourself to understand how it all works, and to gain experience. We have no intention to run an actual website, we just want to experience how it is to make the whole package ourself. Commented Jul 28, 2013 at 21:45
  • Nice - than just gor for it- I myself advocate for a "from scratch" implementation for learning purposes. Commented Jul 29, 2013 at 14:06

1 Answer 1

1
In [1]: users = ["user1", "user2"]

In [2]: pws = ["password", "12354"]

In [3]: dict(zip(users, pws))
Out[3]: {'user1': 'password', 'user2': '12354'}
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent, I am really embarrased I did not know that.
Not knowing some hack isn't a reason to be embarrassed! It's why you have StackOverflow!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.