Coding the Missing Pieces
00:00 Coding the Missing Pieces. In the previous section, you saw the creation of the main glue code for the application. In this part of the course, you’ll see the missing functions created, completing the initial functionality of the checker application.
00:17 The site connectivity checker app will be able to check multiple URLs in every execution. Users will feed URLs into the app by listing them at the command line, providing them in a text file, or both.
pathlib is imported to manage the path to the optional URLs file. Next, a helper function is added with the following code. This defines
urls, which initially stores the list of URLs provided at the command line.
Note that if the user doesn’t supply any URLs, then this will be an empty list. Here, you define a conditional that checks if the user has provided a URLs file. If so, then the
if block augments the list of target URLs resulting from calling
_read_urls_from_file() with the file provided in the
user_args.input_file command-line argument. Finally, the list of URLs is returned.
This line defines a conditional statement that checks if the current file is an actual file in the local file system. To perform the check, the conditional calls the
.is_file() method on the
if block opens the file and reads its content using a list comprehension. The list comprehension strips any leading and trailing whitespace from every line to prevent processing errors later on. This conditional checks if any URL has been gathered.
That’s a reasonably complex program flow, but now you can continue with the final part of
__main__.py. To run connectivity checks over multiple websites, you need to iterate through the list of target URLs, do the checks, and display the corresponding results.
This line starts a
for loop that iterates over the target URLs. Here, you define and initialize an error which will hold the message that will be displayed if the app doesn’t get a response from the target website.
This line calls
display_check_result() with appropriate arguments to display the connectivity check result to the screen. To wrap up this file, you add the typical
if __name__ == "__main__": boilerplate code.
You’ve written a lot of code without seeing it in action. You’ve coded the site connectivity checker CLI and the entry-point script. Now it’s time to give the application a try. Before doing that, make sure you’ve downloaded the included
sample-urls.txt file from the course materials and placed it in the working directory.
If an error occurs during the check, then you get a message on-screen with the information about what’s causing the error. Go ahead and try some other URLs and features. For example, try to combine URLs at the command line with URLs from a text file using the
-f switches. Additionally, check what happens when you provide a URLs file that’s empty or nonexistent.
06:05 The connectivity checker works well, but it has a hidden issue. The execution time can be considerable if you run it with a long list of target URLs. This is because all of the checks run synchronously, waiting for site one to respond before starting the check on site two, and so on. With a long list of URLs and slow site responses, this can add up quickly.
06:28 The solution to this problem is to run checks asynchronously, running multiple checks at the same time. This is possible because the majority of the program’s execution time is waiting for responses from the sites being checked.
Become a Member to join the conversation.