How to Setup a Simple Web Server in Minutes With Python
- By Christopher Kielty, updated Jan 4, 2019
Sometimes I'm working with a small static site and I'd like to serve it locally to test things out. To do this quick and easy I sometimes use Python's SimpleHTTPServer, which basically starts a web server in the current working directory. Actually, I am doing this right now to preview this page as I write it.
python -m SimpleHTTPServer 8000 Serving HTTP on 0.0.0.0 port 8000 ...
python -- the python command.
-m -- Locate a python module.
SimpleHTTPServer -- The Python module we want in this case.
8000 -- The port number.
Yes, SimpleHTTPServer is case sensitive. Typing simplehttpserver returns /usr/bin/python: No module named simplehttpserver
Now open a browser and enter 127.0.0.1:8000. If the current working directory where the server was started has an index then this will show up in the browser. This can be helpful for getting link paths to work as expected relative to the project root.
127.0.0.1 - - [04/Jan/2019 18:34:13] "GET / HTTP/1.1" 200 -
Connections are listed in the console window.
The command for starting the server is different in version 3 of Python. Check which version of python is installed with python --version. I've got Python 2.7.10, so SimpleHTTPServer is just fine.
If using Python 3
python -m http.server 8000