Python: Run a simple http server with Python 3

2020.06.08 | 115 words |

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.

 

About the Ham

Hi I'm Hamilton - I built this! If you want more content like this subscribe to my email list, connect with me around the web, or take a look at some of my other projects.

More Projects

comments powered by Disqus