A common need is to bring up a web server ad-hoc to serve out some files via HTTP. Most of the time I am using this trick when I want to serve a local directly quickly via HTTP without setting up anything for it.

Python has a module http.server which can be started also from the command line:

python -m http.server 8000 --bind

That's all it needs to serve up the current directory via HTTP. If the parameter --bind is omitted, it will bind to all interfaces.

Use cases

  • Some JavaScript code causes trouble when the page is opened directly via a file:/// link. In this case serving it up through a simple web server often does the trick.
  • Quickly sharing something on a local network. Be careful though, better understand what is on your network and which directory you are serving up.

Python 2

You will have to use a different command line if you are only having Python 2 available:

python -m SimpleHTTPServer 8000


comments powered by Disqus