First, you'll need to install the speedtest-cli library for speed testing and smtplib library for sending emails.
pip install speedtest-cli
Secondly, here's a Python script that performs the speed test, sends an email with the result every hour, and triggers an email if the speed goes beyond a specified limit (x_limit).
You can create a file named speed_test.py:
import speedtest
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import time
# Email Configuration
smtp_server = "smtp.yourmailserver.com"
smtp_port = 587
sender_email = "your_email@example.com"
receiver_email = "recipient_email@example.com"
password = "your_email_password"
# Speed Test Configuration
x_limit = 50 # Set your speed limit in Mbps
def send_email(subject, body
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(sender_email, password)
text = msg.as_string()
server.sendmail(sender_email, receiver_email, text)
server.quit()
def perform_speed_test():
st = speedtest.Speedtest()
st.get_best_server()
download_speed = st.download() / 1e6 # in Mbps
upload_speed = st.upload() / 1e6 # in Mbps
return download_speed, upload_speed
while True:
# Perform Speed Test
download, upload = perform_speed_test()
# Email the results
subject = "Hourly Speed Test Results"
body = f"Download Speed: {download:.2f} Mbps\nUpload Speed: {upload:.2f} Mbps"
send_email(subject, body)
# Check if speed is beyond the limit
if download > x_limit:
alert_subject = "High Speed Alert!"
alert_body = f"Download speed ({download:.2f} Mbps) is beyond the limit of {x_limit} Mbps!"
send_email(alert_subject, alert_body)
# Wait for an hour before the next speed test
time.sleep(3600) # 3600 seconds = 1 hour
Please make sure you update the smtp_server, smtp_port, sender_email, receiver_email, and password variables with your email server details. Also, modify x_limit to set your desired speed limit in Mbps.
All the best.