Write a python script to check the status of your websites

What? PowerUserGuide is starting to publish coding tutorials? That’s correct, I’ve decided that it’s time to go back to my roots of sharing what I learn with other people on this blog as a means to help myself reinforce what I am learning.

When you are managing a bunch of websites, you might be interested in knowing whether they are online or not. You could obviously look into online services that help you check if your websites are online. However, most of them cost money whereas creating the solution yourself in Python is free.

If you are looking for a fairly simple way of checking if a website is online, you can use the Python script that I’m going to share with you in this article. Please note that this script only checks the status of your websites when it’s run. If you are the Python scription kind, you could choose to automatically run it periodically and mail you when a website is offline. We’ll leave the option to add that functionality in the future open for now.

The script

First things, first, let’s start by giving you the full script. If you don’t care how the script functions you can easily run the script with the following steps:

  • Make sure Python3 is installed
  • Make sure PIP is installed
  • Install the “request” module
  • Copy the script
  • Make the db.csv file
  • Add the sites you want to monitor to db.csv
import requests
import csv


with open('db.csv') as csvfile:
    csvreader = csv.reader(csvfile,delimiter=";")

    for row in csvreader: 
        url = row[0]
        heartbeat = row[1]
        r = requests.get(f'{url}')

        if r.status_code == 200: 
            print (f'{url}is alive. Checking heearbeat code.')

            if  heartbeat in r.text: 
                print (f"{url} heartbeat found.")
            else:
                print ("Heartbeat not found. Check DB connection.")
        else: 
            print (f('{url} appears to be down.'))

The db.csv file is where the script takes the websites to check on from. There’s two parts to every line:

  • the full URL of the website including the protocol, E.G https://poweruserguide.com
  • The “heartbeat”, which is a text or phrase that’s supposed to be present on the page

The script will not just check whether the website is online, but will also check if it can find the “heartbeat”, the text you want to retrieve from the page. This gives you some extra security that the website is functioning normally.

Analyzing the script

import requests
import csv

This script uses the built-in module CSV. You’ll have to install the python module “requests” through PIP. Requests is a really easy way to make web requests.

with open('db.csv') as csvfile:
    csvreader = csv.reader(csvfile,delimiter=";")

We open the CSV file in Python, and then we use the csv.reader function to read the CSV and turn the content into arrays that Python can use.

    for row in csvreader: 
        url = row[0]
        heartbeat = row[1]
        r = requests.get(f'{url}')

We are looping through the array, and use the first value in the CSV to make a request using the requests.get function. This will send a “get” request to the URL that you’ve provided. We are using an f-string so we can lookup the variable(s). We’ll be using the “heartbeat” variable later in the script.

        if r.status_code == 200: 
            print (f'{url}is alive. Checking heartbeat code.')

            if  heartbeat in r.text: 
                print (f"{url} heartbeat found.")
            else:
                print ("Heartbeat not found. Check DB connection.")
        else: 
            print (f('{url} appears to be down.'))

We’ve already made the request. We are now looking at the reply we are getting from the page to decide what result we should print.

If the status code returned is 200 that means the page is (probably) online. If it’s not, we indicate the page is down.

If the page is online, we also check if we can find the “heartbeat” text on the page. This text is also found in our CSV. If we can’t find the text, we indicate the “heartbeat” can’t be found. I am refering to a database here, because I designed this script to work with Laravel projects and as an extra test I tried to get a value from the database to see if that connection was also working. But you can really write whatever you want here.

And that’s that. If you have this script ready all you need to do is add values to the db.csv file which should be in the same folder. It should look something like this:

https://www.myfirstsite.com;"Words I want to find"
https://www.mysecondsite.com;"Some other words"

And that’s it. Your script is now ready and you can start checking the status of your websites and projects as much as your heart desires!

Discover more from PowerUser Guide

Subscribe now to keep reading and get access to the full archive.

Continue reading