Aug-31-2020, 05:55 AM
Hello, I am currently taking a python course online. My class recently went over making a password checker api. If you take a look below, there is def get_password_leaks_count(hashes, hash_to_check).
How would I turn that tuple comprehension/generator expression into a for loop instead? I've been trying to figure out for the past 2 days and it just seems that I cannot get it right. I have rewatched the videos from the course, but I still cannot figure it out. Can someone show me what the answer would look like?
My attempt:
How would I turn that tuple comprehension/generator expression into a for loop instead? I've been trying to figure out for the past 2 days and it just seems that I cannot get it right. I have rewatched the videos from the course, but I still cannot figure it out. Can someone show me what the answer would look like?
import requests import hashlib import sys password_input = input("Please type in a password you would like to check ").split() def request_api_data(query_char): url = "https://api.pwnedpasswords.com/range/" + query_char res = requests.get(url) if res.status_code != 200: raise RuntimeError(f'Error fetching: {res.status_code}, ' f'check the API and try again.') return res def read_res(response): print(response.text) def get_password_leaks_count(hashes, hash_to_check): hashes = (line.split(':') for line in hashes.text.splitlines()) print(hashes) for h, count in hashes: if h == hash_to_check: return count return 0 print(h, count) def pwned_api_check(password): sha1password = hashlib.sha1(password.encode('utf-8')).hexdigest().upper() # Check password if it exists in API response first5_char, tail = sha1password[:5], sha1password[5:] response = request_api_data(first5_char) print(first5_char, tail) # print(response) # return read_res(response) return get_password_leaks_count(response, tail) def main(args): for password in args: count = pwned_api_check(password) if count: print(f'{password} was found {count} times.' f' Consider changing your password.') else: print(f'{password} was NOT found.') return "complete" if __name__ == '__main__': sys.exit(main(password_file)) # sys.exit(main(sys.argv[1:]) # sys.exit(main(password_input))Yes, I know there should not be same variable names, but this is just to show what I was trying to do.
My attempt:
def get_password_leaks_count(hashes, hash_to_check): for line in hashes.text.splitlines(): hashes = line.split(':') # for h, count in hashes: # if h == hash_to_check: # return count # return 0