Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

Using sleep() to Code an Uptime Bot (Overview)

Have you ever needed to make your Python program wait for something? You might use a Python sleep() call to simulate a delay in your program. Perhaps you need to wait for a file to upload or download, or for a graphic to load or be drawn to the screen. You might even need to pause between calls to a web API, or between queries to a database. Adding Python sleep() calls to your program can help in each of these cases, and many more!

In this course, you’ll learn:

  • The basics of time.sleep()
  • How you can use timeit to measure your code’s execution time
  • How to use time.sleep() to build an uptime bot


Sample Code (.zip)

1.5 KB


Course Slides (.pdf)

3.4 MB

00:00 Hello, my name is Martin. This is a course about Python sleep(): How to Add Time Delays to Your Code. Let’s start this off with a little story.

00:08 So, you’re outside. It’s a beautiful night. You’re lying under the stars and just looking up into the sky, dreaming your thoughts—when suddenly, there’s this one thought that pops into your mind. Is my website down? And you just can’t get it out of your head anymore.

00:23 “Is my website down? Is my website down?” And you start to get more and more anxious. And in this moment, you decide that you never want this to happen again and so you’re going to build a little uptime bot that is going to help you check the status of your website and just notify you if there’s anything wrong with it. So, you can build this little uptime bot, and Python is very helpful and eager and checks for you, and it just keeps checking and checking, but sometimes it gets a little too eager and it just checks too often.

00:52 There’s too little time in between the different checks. And so what you need to do, and what you’re going to learn about in this course, is to help Python take breaks in between and just relax a little and do these requests just as often as it’s necessary so that you can rest assured that you know whether your website is up or down. For this, you’re going to use the time module that is part of the standard library.

01:16 Specifically, you’re going to work with the sleep() function, so you’re going to be using time.sleep() in Python.

01:24 Let’s take a quick look at the table of contents. You will start off by learning how to use time.sleep(). Then you’re going to build the uptime bot—a little script that can check for the status of your website.

01:36 Then you’re going to learn about some next steps on how you can improve this bot to, for example, send emails or to deploy it on the web. I’m going to tell you some resources on Real Python that you can use to keep building on this bot project.

01:51 And because it’s a project about helping your code relax, you’re also going to get some chances to relax yourself. And if your code is going to take a break, you’re also going to be able to take a break. So just relax a bit and enjoy, and I hope you’re going to have a nice time going through this course and learn something. Let’s get started by seeing the uptime bot in action.

02:12 Over here in my command line, I will start the uptime bot, just so you see what you’re going to build at the end of this. You see, when I run this bot, then it pings a website, tells me which URL it is pinging, and it tells me it’s up.

02:27 Then the code takes a little break and calls again to the URL and sees it’s still up, and just keeps going like this. I’m going to stop this.

02:39 And this is what you’re going to build by the end of this course.

02:43 Next up, let’s learn about Python’s time.sleep().

Become a Member to join the conversation.