python 3.10.4
Ubuntu 22.04 LTS (jammy)

Python HTTP Servers module is a great module for testing and lab use when you want to spin off a temporary HTTP Server without installing nginx, apache or any of the HTTP Server Back End.

However it is NOT recommended for production.

To launch the http.server using the -m (execute module as a script option) below is the basic commands. This will launch a http service listening on port 8000 (default port when you did not specify any port).

python3 -m http.server

There are a couple of options that you can use when serving the http.server

OptionDescription
–cgi Run as a CGI server
–bind ADDRESS-b <ADDRESS> specify alternate bind address (default: all interfaces)
–directory-d <Directory> specific alternate directory to be serve (default: current directory)
<port>specify the port number to run the http service on. (Make sure the port you are using does not conflict with any existing port number. (You will get an error if you try to do that)

Example 1: To serve the http server on a different port

Add the port to the end of the python3 -m http.server <port>

python3 -m http.server 12345

Example 2: To change the directory

Use the -d <DIRECTORY> option to change the directory from the current directory to a define directory.

python3 -m http.server -d /home/demo/alternatedirectory 12345