Question

Python 3 http.server: Equivalent of SimpleHTTPServer

pythonpython-3.xhttpserversimplehttpserver

Question

What is the Python 3 equivalent of the following Python 2 command for starting a simple local HTTP server?

python -m SimpleHTTPServer

Short Answer

You will learn why SimpleHTTPServer changed in Python 3 and how to start a local file server with python -m http.server. You will also see how to select a port, serve a different directory, and use the command safely during development.

Concept

In Python 2, the SimpleHTTPServer module could expose files in the current directory over HTTP:

python -m SimpleHTTPServer

In Python 3, this functionality was moved into the http.server module. The direct equivalent is:

python3 -m http.server

The command starts a small HTTP server that serves the files in your current working directory. By default, it listens on port 8000, so you can open this address in a browser:

http://localhost:8000/

This server is useful for local development, previewing static HTML files, testing browser requests, or sharing files temporarily on a trusted local network.

http.server is intentionally simple. It is not designed for production deployment because it does not provide the security, performance, logging, or configuration expected from a production web server.

Mental Model

Think of your current folder as a small public library and python3 -m http.server as the librarian at its entrance.

  • The folder you start in is the library's collection.
  • The server gives the collection a local web address.
  • A browser visits that address and asks for a file.
  • The server finds the matching file and sends it back.

For example, if the folder contains index.html, visiting http://localhost:8000/ lets the server hand that file to the browser.

Take Quiz

Syntax and Examples

The basic Python 3 command is:

python3 -m http.server

On systems where python already means Python 3, this also works:

python -m http.server

To use a specific port, place the port number after the module name:

python3 -m http.server 8080

Then visit:

http://localhost:8080/

To serve files from a directory other than your current one, use --directory:

python3 -m http.server 8000 --directory ./public

Example workflow:

cd my-static-site
python3 -m http.server

If my-static-site contains:

index.html
styles.css
images/logo.png

the server makes those files available beneath http://localhost:8000/. A request for http://localhost:8000/styles.css returns styles.css.

Step by Step Execution

Consider this terminal session:

cd portfolio
python3 -m http.server 8000

Suppose the portfolio folder contains an index.html file.

  1. cd portfolio changes the terminal's current directory to portfolio.
  2. python3 starts the Python 3 interpreter.
  3. -m http.server tells Python to run the built-in http.server module as a program.
  4. 8000 tells the module to listen on TCP port 8000.
  5. The terminal displays a message similar to:
Serving HTTP on 0.0.0.0 port 8000 ...
  1. When you open http://localhost:8000/, your browser sends an HTTP request to the server on your own computer.
  2. The server looks in portfolio for a suitable file, commonly index.html, and returns it.
  3. When finished, press Ctrl+C in the terminal to stop the server.

A browser request may produce a log line such as:

127.0.0.1 - - [10/Jan/2025 12:00:00] "GET / HTTP/1.1" 200 -

200 means the request succeeded.

Real World Use Cases

Common uses for python3 -m http.server include:

  • Previewing a static website: Open local HTML, CSS, and JavaScript through HTTP instead of directly opening an HTML file with file://.
  • Testing JavaScript modules: Browser ES modules often require an HTTP origin rather than a local file URL.
  • Checking API mock files: Serve JSON fixtures such as products.json while building a frontend.
  • Demonstrating a prototype: Quickly let colleagues on a trusted network view static assets.
  • Inspecting generated documentation: Serve a folder created by a documentation tool and navigate it in a browser.
  • Testing downloads: Verify that files have the expected URLs and download behavior during local development.
Take Quiz

Real Codebase Usage

In real projects, developers commonly run a lightweight local server as part of a development workflow.

For a simple static prototype, the built-in server can be enough:

python3 -m http.server 8000 --directory dist

Here, dist is a build output folder containing generated HTML, JavaScript, CSS, and images.

For larger applications, teams usually use framework-specific development servers instead, such as a server provided by Vite, Django, Flask, React tooling, or another build system. Those tools can add features that http.server does not provide, including:

  • Automatic reload when files change
  • Routing fallback for single-page applications
  • API proxying
  • HTTPS development certificates
  • Asset transformations and bundling

Even in these projects, http.server remains convenient for quickly inspecting a directory or reproducing a static-file issue without installing dependencies.

Common Mistakes

Using the Python 2 module name in Python 3

This Python 2 command fails in Python 3 because the module was renamed:

python3 -m SimpleHTTPServer

Use this instead:

python3 -m http.server

Serving the wrong folder

The server exposes the directory where you run the command, not necessarily the folder containing your site.

# This serves the current directory, which may be the wrong one.
python3 -m http.server

Move into the intended folder first, or specify it explicitly:

python3 -m http.server --directory ./public

Forgetting the port in the browser URL

If you start the server on port 8080, this URL is incorrect:

http://localhost:8000/

Use the same port you passed to the command:

http://localhost:8080/

Expecting backend application behavior

http.server serves files. It does not run a Flask, Django, FastAPI, Node.js, or PHP application just because their source files are in the folder.

Using it as a public production server

Do not expose this server to the public internet as a production service. Use a properly configured application server or web server for deployed applications.

Port already in use

If port 8000 is occupied, select another available port:

python3 -m http.server 8081

Comparisons

TopicPython 2Python 3
Module nameSimpleHTTPServerhttp.server
Basic commandpython -m SimpleHTTPServerpython3 -m http.server
Default port80008000
Main purposeServe current-directory filesServe current-directory files
OptionWhen to use it
python3 -m http.serverYou want to serve the current directory on port 8000.
python3 -m http.server 8080You need a different port.
python3 -m http.server --directory publicYour files are in another directory.
Framework development serverYou need live reload, API routes, routing support, or framework integration.

The Python 3 command is a module invocation, not a separate installed program. The -m option means “run this Python module as a script.”

Take Quiz

Cheat Sheet

# Python 3 equivalent of Python 2 SimpleHTTPServer
python3 -m http.server

# Use a custom port
python3 -m http.server 8080

# Serve another directory
python3 -m http.server --directory ./public

# Combine a port and directory
python3 -m http.server 8080 --directory ./public
  • Default port: 8000
  • Default served directory: the current terminal directory
  • Local URL: http://localhost:<port>/
  • Stop the server: press Ctrl+C
  • Python 2 name: SimpleHTTPServer
  • Python 3 name: http.server
  • Use for local development and trusted temporary sharing, not production hosting.

FAQ

What replaced SimpleHTTPServer in Python 3?

Use the http.server module:

python3 -m http.server

What port does Python's HTTP server use by default?

It uses port 8000. Open http://localhost:8000/ in a browser.

How do I run the server on port 3000 or 8080?

Pass the port as an argument:

python3 -m http.server 3000

How do I serve a specific folder with http.server?

Use --directory:

python3 -m http.server --directory ./public

Why does python3 -m SimpleHTTPServer fail?

SimpleHTTPServer is the Python 2 module name. Python 3 moved this feature to http.server.

How do I stop python3 -m http.server?

Focus the terminal running the server and press Ctrl+C.

Is python -m http.server the same as python3 -m http.server?

Only if python on your system points to Python 3. Use python3 when you need to be explicit or when python may refer to another version.

Is Python's built-in HTTP server safe for production?

No. It is a convenience tool for development and simple local use, not a production web server.

Take Quiz

Mini Project

Description

Create a small static profile page and serve it locally with Python's built-in HTTP server. This demonstrates how a directory becomes a locally accessible website without installing a web framework.

Goal

Serve a static HTML page from a project folder and view it at http://localhost:8000/.

Requirements

Create a folder named profile-site.

Add an index.html file with a name, short description, and link.

Start Python's HTTP server from the project folder.

Open the page in a browser using the server URL.

Take Quiz

Keep learning

Related questions

View more