Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Tame Your Uptime Bot

00:00 In the previous lesson, you refactored your uptime bot so that it now lives inside of a function and has a bit of a nicer output, but you maybe also thought about a little problem that might come up.

00:12 So, your bot executes nicely, and it does it in the specified amount of time, and that seems to be all fine. But once it runs into an error, it actually doesn’t stop or tell you anything.

00:24 It just keeps running into more errors without any sort of notification or any end in sight, and that’s not really what you want from your bot. So in this lesson, you’re going to tame your bot a little bit, refactor a bit more, and just add an amount of retries that you want to specify, and after that amount of retries, you want to do something else—for example, send an email to you as the administrator. Back here in the code, what you want to do is add an additional argument to your function that I will call retries. I give it a default value of 3. Now, instead of doing while True: you’re going to say if a value that you could call fails is smaller than the retries, then you want to execute this loop. Now, of course, you need to define this variable as well, so you can say fails starts off as 0,

01:16 and then you need to increment fails somewhere in here. You’re going to do this inside of the except block. So if you’re running into an exception, then you want to say fails += 1.

01:29 Now, your while loop is only going to execute until the code runs into three exceptions. And once that happens, it’s going to jump out of the while loop. So down here, you’re going to want to do something else—for example, send an email to you as the administrator so that you can get notified when your site is actually down, when it’s not just one blip for some reason, but it’s actually consistently trying to reach it three times for your specified amount of time in between. The site just doesn’t come back up, so in that case, you want to send an email. I’m going to point you to resources on how to do this at the end of this course. In this lesson, you added a retries functionality and added the variable fails that makes sure that your code only checks a couple of times before doing something else.

02:14 So your while loop will quit once it ran into three exceptions, and then you want to do something else. And in the next lesson, I’m going to point you to a couple of resources on Real Python where you could implement something like a sending email functionality.

02:28 See you in the next lesson!

Become a Member to join the conversation.