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:]
url = 'https://api.pwnedpasswords.com/range/' + head
res = requests.get(url)
if res.status_code != 200:
if not res.ok:
raise RuntimeError('Error fetching "{}": {}'.format(
url, res.status_code))
hashes = (line.split(':') for line in res.text.splitlines())
@ -43,6 +43,11 @@ def main(args):
pwd = pwd.strip()
try:
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:
foundmsg = "{0} was found with {1} occurrences (hash: {2})"
@ -50,11 +55,6 @@ def main(args):
ec = 1
else:
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