Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import getpass
- def login() -> None:
- username = input('username: ')
- password = getpass.getpass('password: ')
- if authenticate(username, password):
- print('ACCESS GRANTED')
- else:
- print('ACCESS DENIED')
- def authenticate(username: str, password: str) -> bool:
- try:
- stored_password = get_password(username)
- except (FileNotFoundError, LookupError):
- return False
- return stored_password == password
- def get_password(username: str) -> str:
- # if user exists, return user's stored password
- # else raise Exception (file not found or user does not exist)
- username = username.lower()
- passwd = open('passwd')
- for line in passwd:
- # trim the newline
- line = line[:-1]
- uname, pword = line.split(':')
- if username == uname:
- return pword
- raise LookupError('user does not exist')
- if __name__ == '__main__':
- login()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement