Check Website Status and Monitor using Python

FREE Online Courses: Dive into Knowledge for Free. Learn More!

Sometimes we go to a website only to find it down or under maintenance. This can get frustrating real quick when we need some crucial information from the website and that is when a website monitor can help. We simply create a python program to notify us of the status of the website through email. Let us start with Python Website Monitoring Project.

Website monitor in python project:

To implement the project, we need to use libraries that already are in python by simply importing.

Project Prerequisites:

This project is for beginners, hence no prerequisite knowledge is required.

Download Project Code:

You can download the source code from the given link: Website Monitoring Project Code

Project File Structure:

Below is the flow of the project.

  1. Importing necessary libraries
  2. Declaring functions to send emails and monitor website
  3. Read the inputs

Importing necessary libraries:

#Import libraries for Python Website Monitoring project
import urllib.request
import smtplib,time, hashlib

1. Code Explanation:

  • import urllib.request: To open a website and obtain the status, we use urllib’s request.
  • smtplib: To establish an SMTP connection with gmail, we use the library.
  • time: To create the delay to monitor between, we use time.
  • hashlib: To create hashcodes using which we can observe any changes in the website.

2. Declaring functions to send emails and monitor website:

#Function to send email
def send_email(email_string):
   #Fill credentials for sender's email and receiver's email
   email_from = 'enter sender\'s email'
   password = 'enter password'
   email_to = 'enter reciever\'s email'
   #Enter subject line
   subject = "Status of website"
 
   #Connect to gmail's smtp server
   smtp_server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
 
   #Login to the gmail server 
   smtp_server.login(email_from, password)
 
   #Content of email
   message = f"Subject: {subject}\n\n{email_string}"
 
   #Send email through the smtp server
   smtp_server.sendmail(email_from, email_to, message)
 
   #Close the server connection
   smtp_server.close()

Code explanation:

  • def send_email(email_string): Declare the function send_email() with the parameter email_string.
  • Email_from, password, email_to: Enter email details of the sender and the receiver’s email
  • subject: Enter subject of the email. It is optional, but a good practise to keep one.
  • smtp_server = smtplib.SMTP_SSL(“smtp.gmail.com”, 465): Connect to the smtp server of gmail on port 465 using smtplib.SMTL_SSL. SSL is a security certificate
  • smtp_server.login: Connect to the server by logging in using the credentials.
  • Message: Create a message to send in the body of the email
  • smtp_server.sendmail: Send the email using sendemail() with the parameters: sender’s email, receiver’s email and the message to send and close the server connection using close()
#Monitor the website
def monitor_website():
   #Run the loop to keep monitoring  
   while True:
       #Visit the website to know if it is up
       status = urllib.request.urlopen(input_website).getcode()
       #If it returns 200, the website is up
       if status != 200:
           #Call email function
           send_email("The website is down")
       else:
           send_email("The website is up")
           #Open url and create the hash code
           response = urllib.request.urlopen(input_website).read()
           current_hash = hashlib.sha224(response).hexdigest()
           #Revisit the website after time delay
           time.sleep(time_delay)
           #Visit the website after delay, and generate the new website
           response = urllib.request.urlopen(input_website).read()
           new_hash = hashlib.sha224(response).hexdigest()
           #Check the hash codes
           if new_hash != current_hash:
               send_email("The website changed")

Code explanation:

  • def monitor_website(): Declare the function monitor_website() to monitor the website.
  • while True: To keep monitoring until the user terminates the program, create an infinite while loop
  • status = urllib.request.urlopen(input_website).getcode(): Open the website the user wishes to monitor and using getcode(), obtain the status of the website. If it returns 200, it implies the website is up and not otherwise.
  • if status != 200: If the status code is not 220, send an email stating the website being down or so on.
  • current_hash = hashlib.sha224(response).hexdigest(): Create a hash code for the retrieved website using SHA224.
  • time.sleep(time_delay): Wait for a time interval before rechecking the website and obtaining its status
  • response, new_hash: Revisit the website and create a new hash code
  • new_hash != current_hash: Check if the hash codes are similar and send an email if they are not.

3. Read the inputs:

#Read the website and read time interval
input_website = input('Enter the website to monitor: ')
input_website = 'https://'+input_website
time_delay = int(input('Enter the time interval to check website: '))
 
#Monitoring website
monitor_website()

Code explanation:

  • input_website: Read the website to monitor from the user and append ‘https://’ to the given website.
  • time_delay: Read the time interval from the user
  • monitor_website(): Call the function to monitor the website

Python Website Monitoring Output:

Run the website monitoring program and get the output:

python website monitoring output

Summary

We implemented a website monitor that will notify the user when there occurs a change in the website or when a website that was down, comes up.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google | Facebook

5 Responses

  1. Abdelrahman Khaled Abdulmuniem Younis says:

    Please can you give us some examples of websites to visit please as none of entering websites is running the code Please?

  2. Mohamed Manzur Bah says:

    Hello I am hereby to request for help on this kind of problem ocurrs on my code:
    monitor_website()
    File “c:\Users\Library\Desktop\extracting lyric son on python\language_translator\website_monitor\website_monitor.py”, line 16, in monitor_website
    status= urllib.request.urlopen(input_website
    ^^^^^^^^^^^^^
    UnboundLocalError: cannot access local variable ‘input_website’ where it is not associated with a value

  3. Tom says:

    I to have that question, how would we monitor multiple sites that we have in our organization, like internet, sharepoint, web that sort of thing?

  4. Desti says:

    Hi, first things first I wanna say thank you for this insightful codes. If you don’t mind, what about if we want to monitoring a multiple of websites? Please explained it, thank you!

    • Nunna Sai Charan says:

      I think you should create an EXCEL file of the list of websites. And by using pandas we need to make it a dataframe and use it from there. Hope this would be helpful. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *