Putting Everything Together
00:00
Putting Everything Together. So far, your site connectivity checker project has a function that checks if a website is online, and it also has a CLI that you quickly built using the argparse
module.
00:14 In this step, you’ll write the glue code—the code that will bring all of these components together and make your application work as a full-fledged command-line application. First, you’ll start by setting up the application’s main script or entry-point script.
00:29
This script will contain the main()
function and some high-level code that will help you connect the CLI in the front-end with the connectivity checking functionality in the back-end.
00:40
The next step in building the app is to define the entry-point script with a suitable main()
function. To do this, you’ll use the __main__.py
file that lives in the rpchecker
package.
00:51
Including a __main__.py
file in a Python package enables you to run the package as an executable program using the command scene on-screen.
01:01
To start populating __main__.py
with code, open it in your file editor and add the code seen on-screen.
01:12
After importing read_user_cli_args()
from the cli
module, you define the app’s main()
function. Inside main()
, you’ll find a few lines of code that don’t work yet.
01:24
Here’s what the code should do after you provide the missing functionality. This line calls read_user_cli_args()
to parse the command-line arguments.
01:35
The resulting Namespace
object is then stored in the user_args
local variable. This puts together a list of target URLs by calling a helper function that you’ll be coding in a moment. Here, you define an if
statement to check if the list of URLs is empty.
01:53
If that is the case, then the if
block prints an error message to the user and exits the application.
02:00 This line invokes a function, which takes a list of target URLs as an argument and runs the connectivity check over each URL. As the name points out, this function will run the connectivity check synchronously, or one after another. Again, you’ll be coding this function in a moment.
02:20
With main()
in place, you can start coding the missing pieces to make it work correctly. And that’s what you’ll be doing next.
Become a Member to join the conversation.