From 78b38a25b9c1b52b255346ef526d5a84a1d6d83b Mon Sep 17 00:00:00 2001 From: koksnuss Date: Sun, 14 Jul 2019 17:07:47 +0200 Subject: [PATCH] add exception for no connection to the password or mail api --- src/have_I_b33n_pwned.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/have_I_b33n_pwned.py b/src/have_I_b33n_pwned.py index b2b4503..0a1d05b 100755 --- a/src/have_I_b33n_pwned.py +++ b/src/have_I_b33n_pwned.py @@ -21,8 +21,9 @@ # along with this program. If not, see . # updater script for nextcloud # -# TODO +#TODO # - add feature: keepass integration? isnt there something like this already? +# - handle mail API exeption more precisely from os import name @@ -31,7 +32,7 @@ from re import match from argparse import ArgumentParser from hashlib import sha1 from getpass import getpass -from requests import get +import requests if name == 'nt': # disable colors for windows environment @@ -80,12 +81,23 @@ def check_mail_or_password(user_input): query_password(user_input) +def no_connection_to(url): + ''' Warn about no connection to *url* and exit ''' + stdout.write(RED) + print('Could not connect to {}, please check your internet connection'.format(url)) + stdout.write(RESET) + exit(1) + + def query_password(password): ''' Query the first 5 digits of the sha1 hash of the user input ti the haveibeenpwned.com api. ''' password_hash = sha1(password.encode('UTF-8')).hexdigest().upper() request = password_hash[:5] - response = get(PASSWORD_API + request).text + try: + response = requests.get(PASSWORD_API + request).text + except requests.exceptions.ConnectionError: + no_connection_to(PASSWORD_API) hash_searched = 'not yet' for answer in response.splitlines(): data = answer.split(':') @@ -109,7 +121,7 @@ def query_password(password): def query_mail(mail): try: - response = get(MAIL_API + mail).json() + response = requests.get(MAIL_API + mail).json() for breach in response: print('Name: {}'.format(breach['Name'])) print('Title: {}'.format(breach['Title'])) @@ -127,6 +139,8 @@ def query_mail(mail): print('Sensitive: {}'.format('yes' if breach['IsSensitive'] == 1 else 'No')) print('Retired: {}'.format('yes' if breach['IsRetired'] == 1 else 'No')) print('Spam: {}'.format('yes' if breach['IsSpamList'] == 1 else 'No')) + except requests.exceptions.ConnectionError: + no_connection_to(MAIL_API) except: stdout.write(GREEN) print('The mail adress {} is not in the leak database.'.format(mail))