Python Tutorial: Write a Script to Monitor a Website, Send Alert Emails, and Reboot Servers

///Python Tutorial: Write a Script to Monitor a Website, Send Alert Emails, and Reboot Servers

Python Tutorial: Write a Script to Monitor a Website, Send Alert Emails, and Reboot Servers

FavoriteLoadingAdd to favorites

In this Python Programming Tutorial, we’re going to be looking at a real-world example of writing a script to monitor a website. If the website doesn’t return a successful response for any reason then we will send an email and reboot the web server. Let’s get started…

The code from this video (with added logging) can be found at:

Google Account Settings:

Requests Video –

Environment Variables (Windows) –
Environment Variables (Mac and Linux) –

Try/Except Video –

F-String Video –

Crontab Video –

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2019-08-15T22:03:17+00:00August 15th, 2019|Python Video Tutorials|43 Comments

43 Comments

  1. Corey Schafer August 15, 2019 at 10:03 pm - Reply

    I put the code to this script in the description section of the video. For anyone who would like to see what some basic logging would look like for this script, I added some logging code to the script and commented it out for reference. I hope you all find these real-world examples helpful!

  2. Santosh Bandichode August 15, 2019 at 10:03 pm - Reply

    I am grateful for your tutorials and for blah blah blah…blah blah

  3. Victor Chekoff August 15, 2019 at 10:03 pm - Reply

    Awesome tutorial! Do you have any tutorials/code on github on emailing/calendar invites via Outlook from Python

  4. LSU Gamer August 15, 2019 at 10:03 pm - Reply

    I think I skipped your tut on setting up a virtualenv. took me an hour to figure it out on my own.

  5. LSU Gamer August 15, 2019 at 10:03 pm - Reply

    Its "myvenvscriptsactivate.bat" for windows.

  6. Vighnesh Raut August 15, 2019 at 10:03 pm - Reply

    The email spoofing doesn't work. *Sigh*. SMTP doesn't trust easily these days. I really was hoping that this would work as I was going to troll my friends so hard.

  7. PSU Linu x August 15, 2019 at 10:03 pm - Reply

    The question is, what if I want to shutdown my laptop… Everything will stop! Any suggestions to make the script run online?

  8. Mike August 15, 2019 at 10:03 pm - Reply

    Corey, thank you very much for doing these great videos!! also I was wondering how can I know all the arguments that I can use within the .get function. I took a look to the request docs and to the request.py file and I didn't see args names such as timeout. How did you know that you can use it though? Thanks again, I really appreciate it.

  9. Leamon August 15, 2019 at 10:03 pm - Reply

    Awesome tutorial. Thanks a million, Corey!!

  10. Sanjib Bhoi August 15, 2019 at 10:03 pm - Reply

    Very useful video Corey… Thanks a lot

  11. Lightning in My Hands August 15, 2019 at 10:03 pm - Reply

    Windows Users running VSCode – the environment variables your store in your OS can not be accessed by the integrated terminal. Or at least, it's very difficult. Better to create a .env file in the same directory as your source code, and store your variables there.
    Or instead run your terminal outside of VSCode.

  12. Lightning in My Hands August 15, 2019 at 10:03 pm - Reply

    This site is great to follow and understand the SMTP commands – http://www.samlogic.net/articles/smtp-commands-reference.htm

  13. MachineOverlords August 15, 2019 at 10:03 pm - Reply

    Working through an actual project was very helpful. The how-to tutorials are great and I still have ground to cover but I think my biggest issue is how to get started and work through the problem. Thank you for the great content!

  14. حياه جديده August 15, 2019 at 10:03 pm - Reply

    how i can show Facebook notifications and twitter in python

  15. Sonu MsVr August 15, 2019 at 10:03 pm - Reply

    Quality content = instant subscription…😊🤗

  16. David Jones August 15, 2019 at 10:03 pm - Reply

    I have a half finished app on my GitHub a bit like this, it monitors a list of sites with requests and sends and email for any that are down and it runs every min on AWS Lambda. I have another part that uses paramiko which will then ssh into the server of any site that is down to debug the issues. I didn't get any further than that the plan was/is for paramiko to run commands such as check the status of apache, RAM, disk space, mysql etc etc and run re-boot commands accordingly for any that are failing or send further alerts.

  17. praveenraj sr August 15, 2019 at 10:03 pm - Reply

    One of the best tutorial in the entire universe!

  18. Now Or Never August 15, 2019 at 10:03 pm - Reply

    Thanks

  19. Andri Hidayat August 15, 2019 at 10:03 pm - Reply

    I think that I will never unsubscribe your channel because You have a monster power sir, you can do great python programming and also CLI master on Linux. If I can make a request then I hope next time you make video about get how many user connecting to a network and then adjust the equal bandwidth for every hosts using iptables by using python power. Thank you.

  20. Hamid S August 15, 2019 at 10:03 pm - Reply

    Love your mini projects. These are awesome please keep posting

  21. Alexey Bogomolov August 15, 2019 at 10:03 pm - Reply

    Awesome tutorial! It helped me to become familiar with virtualenv development. Also I realized how to work with API of my VPS provider. Did my own server monitoring script with your guidance. https://github.com/movalex/servermon. Also realized I had to add local variables initialization to cron to get it to work. Looking forward to bring it further. Thanks!

  22. Sheshank Shankar August 15, 2019 at 10:03 pm - Reply

    Hey Corey, when will the Flask Heroku deployment video come? I am not very smart and I can't figure it out myself and with other videos. I learned Flask through your series, and I think I will be able to deploy my site if this deployment video comes. Thanks!

  23. PyMoondra August 15, 2019 at 10:03 pm - Reply

    Nice stuff! I have a lot of catching up to do =)

  24. micheal hall August 15, 2019 at 10:03 pm - Reply

    r.ok is a built in method that returns True if status is 200

  25. Kobe Bryant August 15, 2019 at 10:03 pm - Reply

    Hey Corey, since you're uploading general python episodes regurarly now, how about a guide to asyncio in python 3.7?
    Also, I'm getting my first IT-payment this month thanks to You and You're gettng patronized!

  26. Matin R August 15, 2019 at 10:03 pm - Reply

    You have the best python tutorials Corey,
    Thank you so much for the efforts you put into these videos.
    Keep it up

  27. Joseph M Orost August 15, 2019 at 10:03 pm - Reply

    Thank you! Why didn’t you just put your script in an infinite loop and sleep for 10 mins each time around (with some more try blocks in case of sending email fails or the restart fails)?

  28. Klint Krossa August 15, 2019 at 10:03 pm - Reply

    try: except: else: ????

  29. Ranjeet Golkonda August 15, 2019 at 10:03 pm - Reply

    Thank you Corey 🙏

  30. raghav atreya August 15, 2019 at 10:03 pm - Reply

    Hello ,
    Why you are using the node server? Does Django server is not good?

  31. metalmasterlp August 15, 2019 at 10:03 pm - Reply

    amazing videos, please keep it up. Best i've seen on YT so far

  32. Arch Stampton August 15, 2019 at 10:03 pm - Reply

    I particularly appreciated that you indicated why and how you gradually improved your script. Too many times, I find those complicated scripts on github and I wonder what were the steps to get to the final version.

  33. mkcarino 1 August 15, 2019 at 10:03 pm - Reply

    Thank you Corey your videos you are very good at explaining! Just tipped you via Brave!

  34. anony mous August 15, 2019 at 10:03 pm - Reply

    I did something similar recently on my own. I made a python script to scan a company's 'careers' page for a job that I should be getting. It's opening up soon and a former manager is trying to help me get on. It detects how many pages there are(usually around 19 pages of results total) using the pagination text at the bottom of the page, then it will go through every list page and open up every job page on each list page to scan the whole page for a specific keyword. I think it opens like 120 or so job pages. I put like a 5 second delay in between each job page scan so it's not excessive, and in between each complete scan I make it wait around 40-60 minutes so that way it's not constantly loading pages all day. Once it finds the job im looking for, it will use the pushbullet python package using pushbullets api to send a notification to my phone. The pushbullet api is great and easy to use. I could easily add something similar to my reddit scanner, like a breaking news alert for example.

  35. waya208 August 15, 2019 at 10:03 pm - Reply

    Excellent tutorial! Would you consider a version that doesn't use linode and gmail? For example: An intranet that has no access to the Linode or GMail servers.

  36. ExplaineR KhaN August 15, 2019 at 10:03 pm - Reply

    how the youtube ads embed in upload video.?

    how to get that algorithm in django.?

  37. Nathan Tallack August 15, 2019 at 10:03 pm - Reply

    You may want to validate that starttls worked correctly before you pass those auth credentials, and fail out if TLS does not work.

  38. Koodauskanava August 15, 2019 at 10:03 pm - Reply

    Keep it DRY and just put if not r or r.status_code != 200

  39. Sushant Regmi 069 August 15, 2019 at 10:03 pm - Reply

    Legend's on fire 🤗🤗💪

  40. Vivek Teega August 15, 2019 at 10:03 pm - Reply

    Thanks Corey!! Been looking for this since a long time

  41. Rob Mitchell August 15, 2019 at 10:03 pm - Reply

    Corey,
    $ export XYZ=xyz
    $ env | grep XYZ
    XYZ=xyz
    $
    Not showing the other vars you set. Cheers.

  42. Rangarajan V K August 15, 2019 at 10:03 pm - Reply

    You are awesome!!

  43. Fernando Goya August 15, 2019 at 10:03 pm - Reply

    This type of video is also awesome! And the content is really handful. Thanks!

Leave A Comment

*