Python: Run a simple http server with Python 3

Date: 2020-06-08 | dev | http | python | server |

DISCLOSURE: If you buy through affiliate links, I may earn a small commission. (disclosures)

problem

A lot of my projects just need a simple http server for development. I use conda a lot for my Python projects so Python is readily available on my machine. How do I run a simple http server with Python 3 to serve a given directory on localhost?

solution

You can run a simple http server on localhost with Python 3 by running this command in your terminal:

python -m http.server [port]

So, for instance, if I wanted to bind my server to localhost:1228, I'd do:

python -m http.server 1228

By default this will serve the directory your terminal is currently in. You can change the directory being served by adding flag --directory YOURDIRECTORY.

Want more like this?

The best way to support my work is to like / comment / share for the algorithm and subscribe for future updates.