Two tiny changes (#27)

* Use .ok property instead of status-code check against 200

* Tighter try/except handling\nPutting less code into the try block
This commit is contained in:
Alex Povel 2019-07-24 21:40:03 +02:00 committed by Michael Pound
parent e1f6c0af2f
commit 36b6f603b4

View File

@ -29,7 +29,7 @@ def lookup_pwned_api(pwd):
head, tail = sha1pwd[:5], sha1pwd[5:] head, tail = sha1pwd[:5], sha1pwd[5:]
url = 'https://api.pwnedpasswords.com/range/' + head url = 'https://api.pwnedpasswords.com/range/' + head
res = requests.get(url) res = requests.get(url)
if res.status_code != 200: if not res.ok:
raise RuntimeError('Error fetching "{}": {}'.format( raise RuntimeError('Error fetching "{}": {}'.format(
url, res.status_code)) url, res.status_code))
hashes = (line.split(':') for line in res.text.splitlines()) hashes = (line.split(':') for line in res.text.splitlines())
@ -43,6 +43,11 @@ def main(args):
pwd = pwd.strip() pwd = pwd.strip()
try: try:
sha1pwd, count = lookup_pwned_api(pwd) sha1pwd, count = lookup_pwned_api(pwd)
except UnicodeError:
errormsg = sys.exc_info()[1]
print("{0} could not be checked: {1}".format(pwd, errormsg))
ec = 1
continue
if count: if count:
foundmsg = "{0} was found with {1} occurrences (hash: {2})" foundmsg = "{0} was found with {1} occurrences (hash: {2})"
@ -50,11 +55,6 @@ def main(args):
ec = 1 ec = 1
else: else:
print("{} was not found".format(pwd)) print("{} was not found".format(pwd))
except UnicodeError:
errormsg = sys.exc_info()[1]
print("{0} could not be checked: {1}".format(pwd, errormsg))
ec = 1
continue
return ec return ec