python -m http.server: set the port, folder and phone access
Start a quick local web server with Python, choose the port (8000 by default), serve another folder, and decide who on your network can reach it.
python -m http.server serves the files in the current folder over HTTP, with nothing to install. It listens on port 8000 unless you give another number, and by default it listens on all network interfaces, so other devices on your Wi-Fi can open it too.
What to do, step by step
- Open a terminal in the folder that holds your files (the one with index.html).
- Run python -m http.server. If python is not found, try python3 -m http.server, or py -m http.server on Windows.
- Open http://localhost:8000 in your browser.
- To use another port, put the number at the end, for example python -m http.server 5500, then open http://localhost:5500.
- Stop the server with Ctrl+C in the terminal.
Commands you can copy
python -m http.serverpython -m http.server 5500python -m http.server --bind 127.0.0.1python -m http.server --directory /path/to/folderpython -m http.server 5500 --bind 127.0.0.1 --directory ./siteOptions and common problems
- Who can reach it
- The Python docs say the server binds to all interfaces by default. A phone on the same Wi-Fi can open http://YOUR-COMPUTER-IP:8000, and so can anyone else on that network. Add --bind 127.0.0.1 to keep it to your own computer. --bind was added in Python 3.4.
- Serve a different folder
- --directory (or -d) sets the folder to serve. It was added in Python 3.7. Without it, the server uses the folder you ran the command from.
- OSError: Address already in use
- Another program holds that port, often an older copy of the same command. Use another number, or see how to find and stop the program using a port.
- The page shows a file list, not my site
- The folder has no index.html. Put your page in that folder, named index.html, or run the command from the right folder.
- 404 for a file you can see on disk
- The server serves only the folder it was started in and the folders below it. A file outside it cannot be reached.
- Phone cannot open it
- Check both devices are on the same Wi-Fi, the server was not started with --bind 127.0.0.1, and your firewall allows Python on private networks. The open from phone page shows how to find your computer’s address.
- Use it for a real site
- Do not. The Python docs say http.server is not recommended for production and only implements basic security checks. Use it for local testing.
Questions about python -m http.server
- What port does python -m http.server use?
- 8000, unless you pass another number. The Python docs show python -m http.server 9000 as the way to change it.
- How do I run it on port 5500?
- python -m http.server 5500. Port 5500 is also the default of the VS Code Live Server extension, so close one of them if both are running. See the localhost:5500 page.
- Is http.server the same as Live Server?
- No. Live Server reloads the page when you save a file. python -m http.server only serves files, so you refresh by hand.
- Which HTTP version does it speak?
- HTTP/1.0 by default. Python 3.11 added --protocol HTTP/1.1.