From a72100ee2360d6829eba55f54bef4b2f65946d65 Mon Sep 17 00:00:00 2001 From: panki27 Date: Wed, 15 Nov 2017 20:12:53 +0100 Subject: [PATCH] Added basic exception handling --- certAlert.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/certAlert.py b/certAlert.py index f550020..da68264 100644 --- a/certAlert.py +++ b/certAlert.py @@ -1,8 +1,6 @@ #!/bin/env/python -import datetime +import datetime, sys, re, urllib2 from bs4 import BeautifulSoup -import re -import urllib2 TARGET_URL = 'https://www.cert-bund.de/overview/AdvisoryShort' MEMORY_PATH = 'C:\Users\Panki\Desktop\Privat\Dev\certAlert\out.txt' @@ -22,8 +20,12 @@ class Advisory: print('desc: ' + self.description) print('link: ' + self.link) - -response = urllib2.urlopen(TARGET_URL) +try: + response = urllib2.urlopen(TARGET_URL) +except: + e = sys.exc_info()[0] + print('Error getting Webpage!\r\n' + e) + sys.exit('Stopping execution!') html = response.read() soup = BeautifulSoup(html, 'html.parser') @@ -31,10 +33,18 @@ results = [] for adv in soup.find_all('tr', {'class' : re.compile('search-result-*')}): x = Advisory(adv) results.append(x) - -with open(MEMORY_PATH, 'r') as memFile: - checkedIDs = memFile.read() - memFile.close() +try: + with open(MEMORY_PATH, 'r') as memFile: + checkedIDs = memFile.read() + memFile.close() +except IOError: + print('Error reading memory file!') + print('Continuing without list of checked IDs...') + checkedIDs = '' +except: + e = sys.exc_info()[0] + print('An unknown error occured!') + print(e) for result in results: if result.risk > 3: for prog in PROGRAMS: @@ -47,8 +57,4 @@ for result in results: with open(MEMORY_PATH, 'w') as memFile: for result in results: memFile.write(result.identifier + '\r\n') - memFile.close() - - - -#print(soup.prettify()) \ No newline at end of file + memFile.close() \ No newline at end of file