• Brian

Python Requests Module: Simple Website Checker

In this post, I will be using the Python requests module to check if my website is up.

To do this I will send a get request https://briantcarr.com. If the get request returns a value of 200, then all is good. Otherwise, the website is experiencing issues.



I started this by first writing the correct interpreter line. Otherwise, I would have to specify the interpreter every time the file was executed. The interpreter line specified that this python script is to be run through the python3.6 interpreter.


Next, I imported the requests modules with an import statement. After that, I used the get function of the requests modules to send an HTTP get request to the specified URL. In this case, the URL specified was https://briantcarr.com.


Finally, I printed out what was returned from the HTTP get request. Before I was able to execute the code without specifying the interpreter in the command I had to change the file to be executable using chmod +x.



The output specifies that the HTTP request returned an HTTP status code of 200. This signifies that the webpage was up at the time the request was generated.



Next, I wanted to be sure to only output the status code. This was done by modifying the print statement to print the status_code of the variable which contained the HTTP response.



Now that the output was more manageable, I moved on to adding in an if condition which would determine if the website if online or not. If the status_code is 200, then the script will output something letting me know that it is online. If the status code is something else, I should be notified.



The output of this solution can be seen in the screenshot below.



-Brian

11 views0 comments

Recent Posts

See All

Grep!