Serving up a Directory with Python

Learn how Python can be used to serve directories over the network.

We'll cover the following

Serving directories

If we have a directory full of files and we want to serve them over the network, we don’t have to install and configure a web server if we already have Python installed. Python has a built-in web server module we can invoke directly from the command line. This is great for testing a single-page application locally or sharing some files with others quickly.

Let’s try it out. We create a new directory named pythonsite and switch to it:

$ mkdir pythonsite
$ cd pythonsite

Then, we create an HTML page named index.html:

$ cat << 'EOF' > index.html
> <h2>My Website</h2>
> <p>This is served from Python!</p> 

With the file in place, we can start the web server:

$ python3 -m http.server

Click on the “Run” button to practice the above commands.

Get hands-on with 1200+ tech skills courses.