Python Monitor a Web Page

Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

import requests
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def SendEmail():
    host = "SMTP-SERVER"
    sender = ""
    receivers = ""
    subject = "The web site may be down!"

    #Establish SMTP Connection
    #oSmtp = smtplib.SMTP('', 587)
    oSmtp = smtplib.SMTP(host)

    #Start TLS based SMTP Session

    #Login Using Your Email ID & Password
    #oSmtp.login("", "")

    #To Create Email Message in Proper Format
    msg = MIMEMultipart()

    #Setting Email Parameters
    msg['From'] = sender
    msg['To'] = receivers
    msg['Subject'] = subject

    #Email Body Content
    message = """
	Hi All,
    Please check the web site
    #Add Message To Email Body
    msg.attach(MIMEText(message, 'html'))

    #To Send the Email
       print ("Successfully sent email")
    except SMTPException:
       print ("Error: unable to send email")

    #Terminating the SMTP Session

def MonitoWeb(piUrl, piKeyword):
    nCode = 0
    resp = requests.get(piUrl)
    if resp.ok:
        sHtml = resp.text
        if piKeyword in sHtml:
           print ("We've found the string!")
           message = "SOLR may be down!"
           print (message)
           nCode = -1
        print ("Boo! {}".format(resp.status_code))
        print (resp.text)
        nCode = -1

    return nCode

sUrl = ''
sKeyword = "bangtech"

resp = MonitoWeb(sUrl, sKeyword)
if resp != 0:
    time.sleep(1) # Sleep 1 seconds
    resp = MonitoWeb(sUrl, sKeyword)
    if resp != 0:
       message = "SOLR may be down twice!"
       print (message)