Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Refactor Your Uptime Bot

00:00 In the previous lesson, you built out this basic version of the uptime bot. In this lesson, you’re just going to do a bit of refactoring to make the code a bit better, move it into a function, and improve the print() statements.

00:12 You want to start by actually moving this success statement out of the try block. Instead, you want to put it into an else block. You can have else blocks in try/except statements, and the code in here only executes if the try/except block didn’t run into any error.

00:30 So, only then, we want to print out the "success". Now let’s also make the error message a bit more meaningful. I’m going to catch the exception as e, assign it to a variable name, and then create an f-string here where I can print out the error message.

00:47 Let’s say this is the error message for a specific URL. Let’s do something similar down here. Another f-string, and here I’m going to say the URL

01:01 is up! A happy success message. Okay, time.sleep()you can leave that as is. Then let’s move this whole thing into a function. I’m going to indent it one and then create a function definition here, and I’m going to call it uptime_bot(). It takes as input a url.

01:21 The url comes from out here and then the function takes a url and then executes the code logic. Now, instead of defining the url up here, I’m going to also take this one and you’ll move it down into the if __name__ block.

01:36 So you can say if __name__ == "__main__": then you want to have the url defined in here. Then, run the uptime_bot()

01:52 with the url.

01:55 Currently, because I copy-pasted this, I still have the non-working URL, but let’s give it a try whether the code still works as it did before. I’m going to run this by pressing the run button.

02:10 And here, we’re getting an error. It should be error 503, something’s not going great for the URL that ends with the /ddd. It keeps trying, so it runs this many times, every three seconds.

02:23 This is what you’d expect. Now let’s try it again with a valid URL, run the same code again, and this seems to be working fine. Now you’re getting that the Python documentation is up, which is always a good sign to know that the Python documentation is actually live on the internet. All right, so one thing you might notice here is yes, you did limit how often and how quickly the code runs by letting your code sleep, but it still just keeps going and keeps going, which might be fine if you have this deployed somewhere and you just want to constantly check. Maybe you want to change the time how often does it check to a minute or something like that. Let me stop this.

03:05 All right, but if you’re running into an exception, currently it also just keeps running as you saw before.

03:12 So instead of keeping it running all the time if your code encounters an exception, in the next lesson, you’re going to build the uptime bot out a little more to accept an argument where you can say after how many failed attempts it should stop and then do something else instead of keep checking.

Become a Member to join the conversation.