Skip to content

Instantly share code, notes, and snippets.

@ogrigas
Created March 7, 2016 09:16
Show Gist options
  • Save ogrigas/337679e89124dec9338d to your computer and use it in GitHub Desktop.
Save ogrigas/337679e89124dec9338d to your computer and use it in GitHub Desktop.
Example Python script that pings a website and sends email alert in case it's unreachable
from urllib2 import urlopen
def send_alert():
from smtplib import SMTP
from email.mime.text import MIMEText
msg = MIMEText('nuf said')
msg['Subject'] = 'inventi.lt is unreachable'
msg['From'] = 'osvaldas@inventi.lt'
msg['To'] = 'osvaldas@inventi.lt'
server = SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login('username', 'password')
server.sendmail('osvaldas@inventi.lt', ['osvaldas@inventi.lt'], msg.as_string())
server.quit()
try:
urlopen('http://inventi.lt/xxx')
print 'OK'
except:
send_alert()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment