Python Site Connectivity Checker Project
Are you tired of manually checking your website’s connectivity? Do you want to automatically monitor your website’s uptime status? Well, look no further because this article will guide you on how to create a Python site connectivity checker project.
The Python site connectivity checker project is a script that automatically checks the connectivity of websites. It is a handy tool to ensure that your website is up and running. The script will send a request to a specified URL and check if the server responds with an HTTP status code of 200.
To start with the project, we first need to install the necessary library. We will be using the ‘requests’ library, which can be installed by running the following command in your terminal.
pip install requests
After installing the requests library, open your Python editor and start creating your project.
Creating the Project
Create a file named ‘site_checker.py,’ which will contain your Python code.
# Importing necessary Library
import requests
# URL to check
url = 'https://www.example.com'
# Send Request to the URL
response = requests.get(url)
# Print the Response (HTTP Status Code)
print(response.status_code)
In the code above, we imported the ‘requests’ library and assigned a variable ‘url’ to the website that we want to check. We then sent a GET request to the URL using the ‘requests.get’ method and stored the response in a variable called ‘response.’ Finally, we printed the HTTP status code returned by the server.
If the output is ‘200,’ it means that the server responded with a success HTTP status code. The HTTP status codes are standard responses returned by web servers. The ‘200’ status code means that everything is working correctly.
However, if the output is ‘404,’ it means that the server could not find the requested URL, and the server responded with an error HTTP status code of ‘404.’ There are different types of HTTP status codes, and you can learn more about them here.
Automating the Check
To automate the site connectivity check, we need to use a module in Python called ‘schedule.’ The ‘schedule’ module allows us to run a function at a specific time interval.
pip install schedule
Add the ‘schedule’ module and add the following code to the ‘site_checker.py’ file.
# Importing necessary Libraries
import requests
import schedule
import time
# Function that checks the connectivity
def check_connectivity():
# URL to check
url = 'https://www.example.com'
# Send Request to the URL
response = requests.get(url)
# Print the Response (HTTP Status Code)
print(response.status_code)
# Schedule a task every minute to check connectivity
schedule.every(1).minutes.do(check_connectivity)
while True:
schedule.run_pending()
time.sleep(1)
In the code above, we created a function ‘check_connectivity’ that contains the same code that we used earlier to check website connectivity. We then scheduled this function to run every minute using the ‘schedule.every(1).minutes.do’ method. The ‘while True’ loop ensures that the scheduled task continues to run.
Adding Notification
It is essential to receive notifications when our website is down. We can use multiple notification methods such as email, slack, text messages, or push notifications.
For this tutorial, we will be using the slack notification method. Create an incoming webhook in Slack using the guide here. Add the webhook to your Python code, as shown below.
# Importing necessary Libraries
import requests
import schedule
import time
#Slack Webhook
SLACK_WEBHOOK_URL = 'https://hooks.slack.com/services/XXXXX/XXXXX/XXXXXXXXXX'
# Function that checks the connectivity
def check_connectivity():
# URL to check
url = 'https://www.example.com'
# Send Request to the URL
response = requests.get(url)
# If website is down
if response.status_code != 200:
# Send notification to slack channel
message = {
'text': "Website " + url + " is down."
}
response = requests.post(
SLACK_WEBHOOK_URL, json=message
)
if response.status_code != 200:
raise ValueError(
'Request to slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
)
print('Website is down. Notified slack.')
else:
print('Website is up and running.')
# Schedule a task every minute to check connectivity
schedule.every(1).minutes.do(check_connectivity)
while True:
schedule.run_pending()
time.sleep(1)
In the code above, we added a condition to check if the server responses with an HTTP status code other than 200. If the website is down, we send a message to our Slack Channel with the website’s URL. We use the ‘POST’ method to send the message to the Slack Channel using the Webhook URL.
Conclusion
In this article, we have seen how to create a Python site connectivity checker project. We have used the ‘requests’ library to check the website connectivity and the ‘schedule’ library to automate the check. Additionally, we added the functionality of notification if the website is down using the Slack channel. Through this project, you can monitor your website’s uptime status effectively.