keepcalculator

06 Numbers and Powers

Start Web Server Python: Built-in & Simple Options

Card No. 06·74350 · Updated · Standard formula, shown below

To start a web server with Python, you use the built-in http.server module — the command is python -m http.server (or python3 -m http.server on most Linux/macOS systems). This serves the current directory over HTTP on port 8000 by default, giving you an instant, zero-configuration web server for local testing and file sharing.

What the Python http.server Module Does

The http.server module is part of Python’s standard library, meaning it ships with every Python installation — no pip install, no dependencies, no configuration files. When you run python -m http.server, Python starts a basic HTTP server that listens on port 8000 and serves files from the directory where you launched the command. The server handles GET and HEAD requests, lists directory contents when no index.html is present, and supports basic MIME types for common file extensions (HTML, CSS, JS, images, etc.).

This is not a production-grade server — it’s single-threaded, has no authentication, and is not designed to handle heavy traffic. But for local development, sharing files on a LAN, or testing a static site, it’s often the fastest way to get a server running.

How to Start a Python Web Server in Different Situations

The exact command varies slightly depending on your operating system and what you want to serve:

  • Basic local server (current directory, port 8000): python -m http.server
  • Specify a different port: python -m http.server 8080
  • Serve a specific directory: python -m http.server --directory /path/to/folder (Python 3.7+)
  • Bind to a specific IP (e.g., to allow LAN access): python -m http.server 8000 --bind 0.0.0.0
  • Python 2 (legacy): python -m SimpleHTTPServer — only if you’re stuck on Python 2, which is end-of-life.

On Windows, you may need to use py -m http.server if python isn’t in your PATH. On macOS/Linux, python3 is the standard command.

What You Can Use It For (and What You Shouldn’t)

The built-in server is perfect for:

  • Testing static HTML/CSS/JS files locally before deploying
  • Sharing files with colleagues on the same network (run with --bind 0.0.0.0 and share your IP)
  • Quickly serving a folder of images or documents
  • Prototyping an API response with a simple JSON file

Do not use it for:

  • Production websites — it’s slow, single-threaded, and has no security hardening
  • Handling sensitive data — no encryption, no authentication
  • High-traffic applications — it will bottleneck quickly

For real deployments, use a proper WSGI server like Gunicorn or uWSGI behind a reverse proxy like Nginx.

How to Stop the Server and Common Issues

To stop the server, press Ctrl+C in the terminal where it’s running. That’s it — Python’s http.server is a foreground process, so it runs until you interrupt it.

Common problems and fixes:

  • “Address already in use” — another process is on port 8000. Either pick a different port (e.g., python -m http.server 8080) or find and kill the conflicting process.
  • “Permission denied” on port 80 — ports below 1024 require admin/root on most systems. Use a higher port or run with sudo (not recommended for a dev server).
  • Can’t access from another device — you need to bind to 0.0.0.0 and allow the port through your firewall.
  • Files not updating — the server reads from disk on each request, so just refresh your browser. If your browser caches, do a hard refresh (Ctrl+Shift+R).

Alternatives: Flask, SimpleHTTPServer, and One-Line Servers

While http.server is the simplest option, there are other ways to start a web server with Python:

  • Flask — a micro-framework that lets you define routes and serve dynamic content. Install with pip install flask, then run a script that defines app = Flask(__name__) and app.run(). More powerful, but requires writing code.
  • SimpleHTTPServer (Python 2) — the predecessor to http.server; still referenced in old tutorials. Don’t use it unless you’re on Python 2.
  • Twisted — a networking library that can serve files with a one-liner, but it’s overkill for most simple needs.
  • Live-server (Node.js) — not Python, but often used for the same purpose; auto-reloads the browser on file changes.

For pure static file serving, http.server is the winner for simplicity. If you need dynamic routes, Flask is the natural next step.

Security and Safety Notes

When you run python -m http.server, you’re exposing whatever is in the served directory to anyone who can reach the port. If you bind to 0.0.0.0, that includes everyone on your network — and if your firewall is misconfigured, potentially the internet. Always:

  • Serve only the folder you intend to share, not your entire home directory.
  • Use a non-default port if you’re on a shared network to reduce casual scanning.
  • Turn it off when you’re done — don’t leave it running in the background.
  • Never use it to serve sensitive files (passwords, private keys, personal documents).

There’s no built-in authentication, so anyone with access to the port can read and download everything in the served tree.

Frequently Asked Questions

How do I change the port in Python http.server?

Just add the port number as an argument: python -m http.server 8080 will serve on port 8080 instead of 8000. You can use any port from 1 to 65535, but ports below 1024 typically require admin privileges.

Can I use Python’s http.server for production?

No. It’s explicitly documented as a development and testing tool. It’s single-threaded, has no security features, and will crash or become unresponsive under real traffic. Use Gunicorn, uWSGI, or a cloud platform for production.

How do I serve a different directory without cd-ing into it?

Use the --directory flag (Python 3.7+): python -m http.server --directory /path/to/folder. This serves that folder without changing your terminal’s current directory.

Why does my server show a directory listing instead of my HTML?

That’s normal. If there’s no index.html in the served directory, the server generates a listing of files. To get a page, create an index.html file in that folder.

How do I make the server accessible from my phone or another computer?

Run python -m http.server 8000 --bind 0.0.0.0, find your computer’s local IP (e.g., ipconfig on Windows, ifconfig on macOS/Linux), and then visit http://:8000 from the other device. Make sure your firewall allows incoming connections on that port.

Is there a way to add auto-reload to python http.server?

Not built-in. The server reads files from disk on each request, so a browser refresh is enough. If you want auto-reload on file changes, you’d need a tool like watchdog or switch to a Node-based server like live-server.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →