Python http.server - SimpleHTTPRequestHandler - Attributes and Methods

Python http.server - SimpleHTTPRequestHandler - Attributes and Methods

The SimpleHTTPRequestHandler class of http server module directly maps the directory structure to HTTP requests. Work like parsing the requests is done by the base class, that is BaseHTTPRequestHandler.

Attributes of SimpleHTTPRequestHandler 

The class-level attributes of SimpleHTTPRequestHandler are elaborated below.

1. server_version

It is represented as 'SimpleHTTP/" + __version__', wherein the version part is described at the module level.

2. extensions_map

This dictionary maps suffixes into MIME types. The mapping used is case-insensitive 
and uncluses lower-case keys only. For default system mappings, custom overrides are present.

Methods of SimpleHTTPRequestHandler 

1.  do_HEAD()

This method sends out the headers that it would send for an equivalent GET request. The do_GET method will provide more clarity on the same.

2. do_GET()

By understanding the request as a path relative to the current working directory, the request is mapped to a local file.

For example, if the request was mapped to a directory, a file named index.html or index.htm is verified in that directory. If the file is located, the contents are returned; otherwise, the list directory() method is used to construct a directory listing os.listdir() is used in this technique.

The next article will help you know how SimpleHTTPRequestHandler can be used to make a simple webserver to serve files that are relative to current directory.

To know more about http.server module and its classes, refer to the following article: