Build an Uptime Bot
00:00 In this lesson, you’re going to build an uptime bot. Eventually it’s going to look something like this, but don’t get too stuck with this code. You’re going to build it step by step starting now.
00:12
Head over to your favorite text editor. I’m here in VS Code, and I’m still on the terminal in an empty folder. I’m going to create a new file. I’m going to say touch uptime_bot.py
. Now you can see I have this empty file, uptime_bot
. Open it up, and I will also get rid of all this extra stuff. Okay. You’re inside of a file called uptime_bot.py
.
00:42 What I usually like to do, and what I suggest you to do when you start coding up a script, is to just take quick comments of what you want to achieve in here.
00:52 So, you’re going to want to have some sort of loop that queries a URL.
01:01 You’re going to want to catch an exception if it happens.
01:06 You’re going to want your code to take breaks.
01:12
And that sounds about right. These are three tasks that you want the script to accomplish. Let’s get started right away. For querying a URL, you’re going to use the urllib
package, and specifically you need urllib.request
.
01:31
This allows you to make requests, and it’s also part of the standard library, so you won’t need to install anything else. Okay, so you can query a URL by saying urllib.request.urlopen()
and then pass in a URL. Now, you could hard-code a URL in here, but I’m going to put it into a variable that, of course, you’re going to need to define as well.
01:55 So, let’s say the URL that you want to query is the Python docs, docs.python.org. And then we go to 3, of course. All right, so that’s the URL that you want to query. Now, feel free to put in your own URL here instead. Now with this, I can send a HTTP query to the URL, and then if there is no exception, then everything is fine, right?
02:25
But now you want to also make sure that you know if there is an exception and catch it, so we’ll wrap this into a try
/except
block.
02:34
Try doing this request, and if there’s an exception, then we want to catch this exception. Now, maybe we want to say, if this try works out, I’m going to print out that everything’s fine. Let’s say it’s a "success"
.
02:51
And then if it’s not fine, print out "error"
.
02:59
Okay, like this, we’re catching an exception and then we have a loop that queries the URL. Okay, we’re querying the URL so far, but now you want to also add a loop. For this, you’re going to just use a while True:
loop and then move all of this functionality into the body of the loop.
03:17
So now you have a loop that queries the URL, and it tries to make this request to the URL, prints "success"
if it works out, and if it runs into an error, then it’s going to instead print "error"
. Now, if you run this as is right now, then this while True:
is going to make the problem where it just keeps calling way too often and way too quickly, so this is where time.sleep()
comes in.
03:41
You want to say, at the end of one of these try
/except
execution runs, you want to say time.sleep()
and let Python rest for 3
seconds.
03:52
Now, you see my editor already tells me that I’m missing time
because I haven’t imported yet, so you can go up here and say import time
. Now everyone is happy, and Python is going to run into an infinite loop and keep trying to request the URL, print "success"
if it finds it and if there’s no error, and print "error"
if there’s an error. And we’re sleeping between calls, so I can get rid of this too.
04:21 All right, let’s give it a quick try.
04:27
So, when I now say python uptime_bot.py
and run it… okay! Success. Three seconds of sleep, and another success
, three seconds of sleep, and another success
. So this is working as expected.
04:43 Now let’s quickly check what happens if it runs into an error. So I’m just going to add something to this URL to create a URL that doesn’t exist and then run the bot again.
04:53
Okay! So instead, it’s running into an error, it prints out error
, waits for three seconds, prints out error
again, et cetera.
05:01
It looks like this is working as expected. Now in the next lesson, you’re going to refactor this code a little bit, add some more interesting print()
statements, and move it into a function.
Become a Member to join the conversation.