From 540f3861ca7ebafe73e422567f73652ee0b8be1d Mon Sep 17 00:00:00 2001 From: Jan Oliver Oelerich Date: Fri, 12 Oct 2012 10:41:42 +0200 Subject: [PATCH] mail checker does not fail anymore, when internet connection is missing --- mailchecker.py | 28 +++++++++++++++------------- statushandler.py | 9 +++++++++ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/mailchecker.py b/mailchecker.py index 6f1d391..721a5f5 100644 --- a/mailchecker.py +++ b/mailchecker.py @@ -5,6 +5,7 @@ import sys import json from datetime import datetime,timedelta import imaplib +from statushandler import has_internet_connection class MailChecker(object): @@ -73,20 +74,21 @@ class MailChecker(object): datetime.now() - timedelta(seconds=self.pause) def get_connection(self): - if not self.connection: - try: - self.connection = self.imap_class(self.host, self.port) - self.connection.login(self.username, self.password) - self.connection.select() - except Exception: - self.connection = None - - try: - self.connection.select() - except Exception,e: - - print e + if not has_internet_connection(): self.connection = None + else: + if not self.connection: + try: + self.connection = self.imap_class(self.host, self.port) + self.connection.login(self.username, self.password) + self.connection.select() + except Exception: + self.connection = None + + try: + self.connection.select() + except Exception,e: + self.connection = None return self.connection diff --git a/statushandler.py b/statushandler.py index 59614ee..ef29007 100644 --- a/statushandler.py +++ b/statushandler.py @@ -2,6 +2,7 @@ import sys import json +import urllib2 class I3statusHandler: modules = [] @@ -61,3 +62,11 @@ class I3statusHandler: # and echo back new encoded json self.print_line(prefix+json.dumps(j)) + + +def has_internet_connection(): + try: + response=urllib2.urlopen('http://74.125.113.99',timeout=1) + return True + except urllib2.URLError as err: pass + return False \ No newline at end of file