Modern Python Projects Transcripts
Chapter: Let's build a CLI
Lecture: Daemon mode and multiple URLs
0:00 Okay, so we have the first version working.
0:03 Now let's change it, so it can accept multiple URL and ping them regularly.
0:08 First, we will add a daemon mode so our program can keep running indefinitely.
0:14 daemon is a type of a program that runs in the background of your computer.
0:18 Since our python code will still run in the terminal,
0:21 it's technically not a daemon, but simply a loop.
0:24 But I like the daemon name because it better reflects what our program is supposed to
0:28 do. So we have to add an option flag to our check command and here
0:39 we can specify a daemon option or a shortcut just -d the I always make a
0:45 spelling mistake in the word daemon.
0:46 So this -d is going to help me a lot.
0:49 By default, we wanted to be set to false,
0:53 and we also want to use it as a flag so we don't want to write
0:57 --daemon = true. We wanted to do something whether or not we specify
1:02 this option. So we said is_flag = True, now inside our function,
1:09 we want to start a while loop.
1:13 We want to check the status coad of URL.
1:16 And if it's not a daemon mode,
1:18 we want to stop. I forgot to pass the daemon here.
1:27 But if the value of daemon flag is true,
1:30 we want to sleep for a few seconds and then run again.
1:33 So let's sleep for five seconds.
1:35 This is undefined. So we need to import sleep from the time module and let's
1:44 sort the imports. Well, let's check if this works, cool
2:01 It seems to be working. It's pinging my website,
2:03 and then it sleeps for around five seconds and then it's spinning again.
2:09 Now how can we accept multiple URL's click that argument except another parameter called
2:15 nargs. But you can specify how Many arguments it can accept.
2:19 If you want to accept unlimited number of arguments,
2:22 you just provide nargs = -1.
2:27 Well, let's rename URL to URLS.
2:36 And then inside our while loop,
2:38 we want to track all the URLS,
2:45 and that should work. Let's go back to the terminal and try some more.
2:49 URLS. Nope.
3:03 That's not going to work. What did I break?
3:07 What if we use only one?
3:09 No. Still nothing, URL okay.
3:12 Yeah,okay, One is working and all of them are working to.
3:21 And let's try the daemon mode.
3:30 Perfect. So everything seems to be working fine.
3:33 We have our uptimer script that we can use to check if a specific website
3:38 is still up and running or if it's returning some error code.