Checking a Website's Connectivity
00:05 At this point, you should have a suitable Python virtual environment with your project’s dependencies installed in a project directory containing all the files that you’ll use throughout the course. So it’s time to start coding.
__version__ module-level constant holds your project’s current version number because you’re creating a brand-new app. The initial version is set to
0.1.0. With this minimal setup, you can start implementing the connectivity-checking functionality.
There are several Python tools and libraries that you can use to check if a website is online at a given time. For example, a popular option is the
requests third-party library, which allows you to perform HTTP requests using a human-readable API.
requests has a drawback of installing an external library just to use a minimal part of its functionality. It would be more efficient to find an appropriate tool in the Python standard library. With a quick look at the standard library, you’ll find the
urllib package, which provides several modules for handling HTTP requests.
urlopen() function takes a URL and opens it, returning its content as a string or
Request object. But you just need to check if the website is online, so downloading the entire page would be wasteful.
This module provides the
HTTPConnection class, representing a connection to a given HTTP server.
HTTPConnection has a
.request() method that allows you to perform HTTP requests using the different HTTP methods. For this project, you can use the
HEAD HTTP method to ask for a response containing only the headers of the target websites.
Now you can go and do some quick tasks. Go ahead and run the following code in a Python interactive session. First,
HTTPConnection is imported, and then a connection instance is made targeting the
pypi.org website using port
80, which is the default HTTP port.
timeout argument provides the number of seconds to wait before timing out the connection. Next, you perform a
HEAD request on the site’s root path,
"/", using the
.request() method. To get the actual response from the server, you call
.getresponse() on the
This starts a
for loop over the HTTP and HTTPS ports. This way, you can check if the website is available on either port. This creates an
HTTPConnection instance using
timeout as arguments. Here, you define a
finally statement. The
try block attempts to make a
HEAD request to the target website by calling the
.request() method. If the request succeeds, then the function returns
True. If an exception occurs, then the
except block keeps a reference to that exception in
site_is_online() function returns
True if the target website is available online. Otherwise, it raises an exception pointing out the problem it encountered. This latter behavior is convenient because you need to show an informative error message when the site isn’t online.
Here, you call
site_is_online() with a non-existing website as a target URL. In this case, the function raises an exception that you can catch later and process to display an error message to the user.
06:42 You’ve implemented the application’s main functionality of checking a website’s connectivity. Now you can continue with the project by setting up its command-line interface, and that’s what will be in the next part of the course.
Become a Member to join the conversation.