Created
November 16, 2018 11:25
-
-
Save python012/b31779aa8dcd2e434365f776d4e6d667 to your computer and use it in GitHub Desktop.
new version, work with python 3.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
import smtplib | |
import config | |
import urllib.request as urllib2 | |
import time | |
import base64 | |
last_email_sent_time = None | |
error_info = None | |
catch_exception = False | |
def send_email(text): | |
global last_email_sent_time | |
if last_email_sent_time is None: | |
last_email_sent_time = time.localtime() | |
else: | |
now = time.localtime() | |
if now.tm_hour - last_email_sent_time.tm_hour > 2: | |
last_email_sent_time = now # only send out email at the first time or 1 h later | |
else: | |
print("Less than 1 hour since last email alert, no email this time. - " + get_time()) | |
return | |
print("Try to send email - " + get_time()) | |
host_server = config.host_server | |
subject = config.subject + " - " + get_time() | |
to = config.to | |
from_ = config.from_ | |
body = "\r\n".join(( | |
"From: %s" % from_, | |
"To: %s" % to, | |
"Subject: %s" % subject, | |
"", | |
text)) | |
try: | |
server = smtplib.SMTP() | |
print(host_server) | |
print(body) | |
server.connect(host_server, config.sent_port) | |
server.starttls() | |
server.login(config.email_account, base64.b64decode(config.email_pw.decode()).decode()) | |
server.sendmail(from_, to, body) | |
server.quit() | |
print("Finish sending email - " + get_time()) | |
except Exception as err: | |
print("Exception on sending email at" + get_time()) | |
print(err.args) | |
# raise | |
def connect_to(url): | |
try: | |
urllib2.urlopen(url, timeout = 10) | |
return True | |
except urllib2.URLError as urlerr: | |
return False | |
except Exception as err: | |
print("Exception - " + get_time()) | |
global error_info | |
global catch_exception | |
error_info = str(err) | |
catch_exception = True | |
return False | |
def check_network(url): | |
bad_network = 0 | |
for i in range(4): | |
if not connect_to(url): | |
print("[" + str(i) + "] " + "check_network: fail to open " + url + " at " + get_time()) | |
bad_network += 1 | |
time.sleep(5) | |
else: | |
break | |
if bad_network == 4: | |
return False | |
else: | |
return True | |
def get_time(): | |
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) | |
def get_email_text(): | |
global catch_exception | |
if not catch_exception: | |
return "Hello! Google is disconnected at " + get_time() | |
else: | |
catch_exception = False | |
return "Hello! Exception is found at " + get_time() + "\n" + error_info | |
if __name__ == '__main__': | |
print("VPN Monitor is started at " + get_time()) | |
while True: | |
if not check_network(config.google): | |
send_email(get_email_text()) | |
else: | |
print("Google is ok at " + get_time()) | |
time.sleep(90) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment