Sunday, 6 September 2020

Python 3: Basic Website Monitor

A bit like the multiple ping test monitor I wrote in PowerShell back in 2015. Here’s my Python 3: Basic Website Monitor.

Very simple to use, just save the script below as say websiteChecker.py and then run the script like -

python websiteChecker.py www.website1.com www.website2.com

- where the arguments are the website you want to monitor (no limits on the number of website you want to monitor.)

In the example below I’ve run -

python websiteChecker.py www.furruri.com www.porsche.com www.jagwar.com www.lambo.com

(with some intentional spelling mistakes)

Image: Example of the Python 3 Basic Website Monitor in action


If you missing some of the Python libraries, check out the previous post here

The Script

'''

websiteChecker.py

This program expects websites as arguments, and will add https:// to the name.

Example> python websiteChecker.py www.furrari.com www.porsche.com

'''


import sys

import time

import colorama

import urllib.request

from termcolor import *

colorama.init()


def webCheck(url):

  url = "https://" + url

  try:

    status_code = urllib.request.urlopen(url).getcode()

    return("OK(" + str(status_code) + ")")

  except urllib.error.URLError:

    return("FAILED")


# Create status array...

status = [''] * len(sys.argv)

x = 1

for webSite in sys.argv[1::]:

  status[x] = "UNTESTED"

  x += 1


chkNext = 1 # Website to check next

loop = "Y"

while loop:

  print(chr(27) + "[2J") # CLS

  x = 1

  for webSite in sys.argv[1::]:

    if status[x] == "UNTESTED":

      cprint(status[x].ljust(10) + " - " + sys.argv[x].ljust(25),'white','on_blue')

    if status[x].startswith("OK"):

      cprint(status[x].ljust(10) + " - " + sys.argv[x].ljust(25),'white','on_green')

    if status[x] == "FAILED":

      cprint(status[x].ljust(10) + " - " + sys.argv[x].ljust(25),'white','on_red')

    x += 1

  print("\nWebsite to check next:\n" + str(sys.argv[chkNext]))

  time.sleep(1)

  status[chkNext] = webCheck(sys.argv[chkNext])

  chkNext += 1

  if chkNext >= len(sys.argv):

    chkNext = 1

4 comments: