How to Start an HTTP Server in Python

Published Sep 18, 2021  ∙  Updated May 2, 2022

What’s the easiest way we can start a local HTTP server in Python?

Once we’re in the terminal at the root directory of our application, we can execute either of these commands, depending on the Python version.

# Python 2
python -m SimpleHTTPServer 8000
# Python 3
python -m http.server 8000

Then, our files will be served from http://localhost:8000/.

For the most part, we can use any port we’d like (not just 8000). On Linux, we’ll find that ports below 1024 are privileged ports, meaning the applications listening on these ports should be privileged. They should run as root or have the CAP_NET_BIND_SERVICE capability.

So, most ports above 1024 are fair game.