[Most recently updated: Sep 29, 2024.]

One of the sweet things that comes with a Python installation is the ability to start up an http server in just one line. Since just about every modern computer (not counting tablets and phones!) has Python, you almost certainly can start up an http server to serve some files in a chosen directory with one quick line of code.

Go to your terminal. Go to the directory you want to serve HTML files from, then, assuming you have Python 3 installed, type:

1
python -m http.server

Note that the output on your terminal will say:

1
Serving HTTP on 0.0.0.0 port 8000 ...

The mention of 0.0.0.0 does not mean the web server is listening at that IP address. Rather, it will be listening at 127.0.0.1:8000. The 0.0.0.0 signifies that the web server will respond to requests that end up on that machine no matter what hostname or IP address that was requested.

Note that you can start your web server at port nnnn with:

1
python -m http.server nnnn

Of course, starting an http server can be made even more convenient by an alias in your .bashrc (or the equivalent for your OS/shell). A commenter noted that he did so, so I thought I might as well add it to the main post:

1
alias http="python -m http.server"

which lets you start a server on port nnnn for the current directory with:

1
http nnnn

This post has been updated a few times to include improved one-liners pointed out by readers as well as for clarity and completeness. I’ve deleted the original text since it’s quite obsolete now. I’ve also finally, in 2024, stopped showing how to do it in Python 2!