Using python -m http.server to Start a Local Web Server
python -m http.server is a simple command used to start a local web server using Python.
It allows you to serve files such as HTML, CSS, JavaScript, images, and other resources
from a folder so they can be accessed through a web browser.
Basic Command
python -m http.server
This command:
- Starts a local HTTP server
- Serves files from the current directory
- Uses port 8000 by default
After running the command, open a browser and go to:
http://localhost:8000
or
http://127.0.0.1:8000
You will see a directory listing of the folder, and you can open your HTML files directly in the browser.
Example Workflow
Suppose you have the following project folder:
myproject/
index.html
script.js
style.css
Open a terminal inside that folder and run:
python -m http.server
Then visit:
http://localhost:8000/index.html
Your page will load just like a normal website.
Why Developers Use It
1. Testing HTML and JavaScript Projects
Some JavaScript features do not work properly when files are opened using the
file:// protocol directly from the system.
Examples include:
- Fetch API
- AJAX requests
- JSON file loading
- JavaScript modules
Running a local server solves these issues.
2. Quick Local Hosting
Instead of installing full web servers like Apache or Nginx, Python provides a quick temporary server for development and testing.
3. Testing Web Simulators
This is especially useful when testing interactive web tools such as:
- DSP simulators
- JavaScript visualization tools
- Interactive educational webpages
Running on a Different Port
You can specify a custom port number:
python -m http.server 9000
Then open:
http://localhost:9000
Serving a Different Folder
You can also specify a directory to serve:
python -m http.server --directory mysite
Python 2 vs Python 3
Python 2 command:
python -m SimpleHTTPServer
Python 3 command:
python -m http.server
What Does -m Mean?
The -m flag tells Python to run a module as a script.
Run the http.server module directly from the Python standard library.
Stopping the Server
To stop the server, press:
CTRL + C
Opening Web Projects With and Without a Local Server
When developing web projects, files can be opened directly from the file system
using the file:// protocol or served through a local server such as
python -m http.server. While both approaches allow you to view HTML
pages in a browser, there are important differences in functionality.
Comparison: With Server vs Without Server
| Feature | Without Server (file://) | With Server (http://localhost) |
|---|---|---|
| Open basic HTML pages | ✔ Works | ✔ Works |
| Load CSS styles | ✔ Works | ✔ Works |
| Run simple JavaScript | ✔ Works | ✔ Works |
| Fetch API / AJAX requests | ✖ Often blocked | ✔ Works |
| Load JSON files | ✖ Blocked by browser security | ✔ Works |
| JavaScript ES modules | ✖ Often fails | ✔ Works |
| Relative API requests | ✖ Not supported | ✔ Works |
| Testing real web behavior | ✖ Limited | ✔ Accurate |
| Access from other devices | ✖ Not possible | ✔ Possible on local network |
Summary
Opening files directly using file:// is fine for simple static pages.
However, many modern web features require a server environment. Using a local server
such as python -m http.server ensures that the project behaves the same
way it would on a real website.