Home >Backend Development >Python Tutorial >Technology to monitor website availability based on Python script in Linux environment
Technology for monitoring website availability based on Python scripts in Linux environment
Abstract: This article introduces how to use Python scripts to monitor website availability in a Linux environment. Specifically, it includes detecting whether the website is accessible by sending HTTP requests and parsing responses, and how to configure the monitoring script as a scheduled task and send alarm emails.
Preparation
In order to use Python to monitor website availability, we first need to install the Python environment. On Linux, you can install Python through the package manager. For example, on Debian/Ubuntu you can use the following command to install:
$ sudo apt-get install python
import requests def check_website(url): try: response = requests.get(url) if response.status_code == 200: print(f"Website {url} is accessible.") else: print(f"Website {url} is not accessible. Status code: {response.status_code}") except Exception as e: print(f"An error occurred while accessing website {url}:", str(e)) def main(): websites = [ "http://www.example1.com", "http://www.example2.com", "http://www.example3.com" ] for website in websites: check_website(website) if __name__ == "__main__": main()
In the above code, we first send an HTTP GET request through requests.get(url)
and get the response. Then, we can get the response status code through response.status_code
and make a judgment.
cron
tool, which can help us implement the function of scheduled tasks. You can use the following command to edit the crontab
file:
$ crontab -e
Then, add the following content in the file:
*/5 * * * * python /path/to/monitor_script.py >> /path/to/log_file.txt 2>&1
The above configuration Indicates that the Python script will be executed every 5 minutes and the output will be redirected to the log file.
First, we need to configure the relevant information of the SMTP server, such as server address, port number, user name and password, etc. Then, we can use the smtplib
library to implement the email sending function.
The following is a modified code example:
import requests import smtplib from email.mime.text import MIMEText def check_website(url): try: response = requests.get(url) if response.status_code == 200: print(f"Website {url} is accessible.") else: send_alert_email(url, response.status_code) except Exception as e: print(f"An error occurred while accessing website {url}:", str(e)) def send_alert_email(url, status_code): smtp_server = "smtp.example.com" smtp_port = 25 smtp_username = "your_username" smtp_password = "your_password" sender = "sender@example.com" receiver = "receiver@example.com" subject = f"Website {url} is not accessible!" message = f"Status code: {status_code}" msg = MIMEText(message) msg["Subject"] = subject msg["From"] = sender msg["To"] = receiver with smtplib.SMTP(smtp_server, smtp_port) as server: server.login(smtp_username, smtp_password) server.sendmail(sender, receiver, msg.as_string()) def main(): websites = [ "http://www.example1.com", "http://www.example2.com", "http://www.example3.com" ] for website in websites: check_website(website) if __name__ == "__main__": main()
In the above code, we first define the SMTP server information, sender and recipient required to send mail. Then, we use smtplib.SMTP
to log in to the SMTP server and send emails.
Summary: This article introduces how to use Python scripts to monitor website availability in a Linux environment. By sending an HTTP request and parsing the response, we are able to determine whether the website is accessible. At the same time, we also introduced how to configure the monitoring script as a scheduled task and send an alarm email when the website is inaccessible. These methods can help you understand and solve website usability problems in a timely manner, and improve user experience and business operation results.
The above is the detailed content of Technology to monitor website availability based on Python script in Linux environment. For more information, please follow other related articles on the PHP Chinese website!