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