Website Status Monitor in Python
Late Night Havoc!
Just as a regular late Sunday night, coming back from visiting the parents, I log into each of my wordpress sites and run whatever plugins or themes need to be updated. Everything was going as routine until i noticed one of the plugins was stuck in a installing state. After waiting a few minutes and seeing that there definitely is an issue, I refreshed the browser and notice that my website received a 500 Internal Server Error. The 500 Internal Server Error is a very general HTTP status code that means something has gone wrong on the website’s server, but the server is not specific to what the issue could be. Scrambling through multiple troubleshooting steps and refreshing the browser over and over to see if each change I made fixed the issue, I decided enough is enough and wanted to somehow automate this step and reduce redundancy. So I created this website status monitor in python below to monitor the status code of the website.
Dissecting The Script
First I imported the requests module. Requests allows you to send organic, grass-fed HTTP requests, without the need for manual labor. There’s no need to manually add query strings to your URLs, or to form-encode your POST data. Then I imported the Time module which provides various time-related functions. Lastly the
datetime module was imported. The
datetime module supplies classes for manipulating dates and times in both simple and complex ways.
I created a while loop to keep the scripting running until the site was back up. The r variable simply tries the access the website i provided in the get function. Then i set an if statement so if the status code of the website was not equal to 200, tell me the current status code the website is displaying and tell me the time the script checked to see. I made sure it ran every 5 seconds to not overdo it with multiple website request to the site. Lastly when the site reached a status code of 200, meaning everything is ok, the script would tell me that its back up and it would end the loop.
Download the script on Github
Interested in Learning Python?
Python Crash Course is a fast-paced, thorough introduction to Python that will have you writing programs, solving problems, and making things that work in no time.
In the first half of the book, you’ll learn about basic programming concepts, such as lists, dictionaries, classes, and loops, and practice writing clean and readable code with exercises for each topic. You’ll also learn how to make your programs interactive and how to test your code safely before adding it to a project. In the second half of the book, you’ll put your new knowledge into practice with three substantial projects: a Space Invaders–inspired arcade game, data visualizations with Python’s super-handy libraries, and a simple web app you can deploy online.
This post “Website Status Monitor in Python” is located under the Programming Category