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 = "info@bangtech.com"
    receivers = "webmaster@bangtech.com"
    subject = "The web site may be down!"

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

    #Start TLS based SMTP Session
    #oSmtp.starttls() 

    #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
    try:
       oSmtp.send_message(msg)        
       print ("Successfully sent email")
    except SMTPException:
       print ("Error: unable to send email")

    #Terminating the SMTP Session
    oSmtp.quit()



sUrl = 'https://www.bangtech.com'
sKeyword = "totalResults"

resp = requests.get(sUrl)
if resp.ok:
    sHtml = resp.text
    if sKeyword in sHtml:
       print ("We've found the string!")
    else:
       message = "The web site may be down!"
       print (message)
       SendEmail()
else:
    print ("Boo! {}".format(resp.status_code))
    print (resp.text)