NeuroAgent

Python 3 SimpleHTTPServer Replacement: Ultimate Guide

Discover the Python 3 equivalent of SimpleHTTPServer with our complete guide. Learn how to use python3 -m http.server for local file serving, command options, and advanced customization. Perfect for developers transitioning from Python 2.

Question

What is the Python 3 equivalent of python -m SimpleHTTPServer?

NeuroAgent

The Python 3 equivalent of python -m SimpleHTTPServer is python3 -m http.server. This command starts a simple HTTP server that serves files from the current directory on port 8000 by default, providing the same functionality as the deprecated SimpleHTTPServer module but updated for Python 3 compatibility.

Contents

Basic Usage

The most straightforward replacement for python -m SimpleHTTPServer in Python 3 is:

bash
python3 -m http.server

This command starts an HTTP server on port 8000 and serves files from the current directory. When you open a web browser and navigate to http://localhost:8000, you’ll see a directory listing of the current folder, allowing you to browse and download files.

According to the Python documentation, the server automatically looks for and serves index.html when someone requests a directory. The terminal output will show something like:

Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

To specify a different port, simply add the port number as an argument:

bash
python3 -m http.server 9000

Command Line Options

The http.server module provides several useful command-line options that extend its functionality:

Port Specification

You can specify any available port number:

bash
python3 -m http.server 8080

Address Binding

The -b or --bind option allows you to specify which network interface to bind to. As noted in the Stack Overflow answer, the server binds to all interfaces by default, but you can restrict it:

bash
python3 -m http.server -b 127.0.0.1

This binds the server to localhost only. Python 3.8+ also supports IPv6 addresses in the bind argument.

Directory Serving

The -d or directory option lets you serve files from a specific directory instead of the current one:

bash
python3 -m http.server -d ~/Documents/website

As mentioned in the Real Python guide, you can associate the server’s home address (/) with a completely different directory using this parameter.

Advanced Features and Customization

For more advanced functionality, you can create custom request handlers by subclassing SimpleHTTPRequestHandler. The DigitalOcean tutorial explains that this approach allows you to:

  • Handle POST requests
  • Add custom headers
  • Override default methods
  • Define custom server behavior

Here’s an example of a custom handler that always serves index.html:

python
import http.server
import socketserver

PORT = 8000

class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        self.path = 'index.html'
        return http.server.SimpleHTTPRequestHandler.do_GET(self)

Handler = MyHttpRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("Http Server Serving at port", PORT)
    httpd.serve_forever()

Programmatic Usage

You can also use the http.server module programmatically without the command line interface. This gives you more control over the server configuration:

python
import http.server
import socketserver

PORT = 8080
Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving the website at port", PORT)
    httpd.serve_forever()

As shown in the Stack Overflow example, this approach allows you to integrate the HTTP server into larger applications.

Comparison with SimpleHTTPServer

The Python documentation states that the SimpleHTTPServer module has been merged into http.server in Python 3. According to the Better Stack Community guide, the main differences include:

  • Cleaner output: The http.server module provides clearer messages and doesn’t show all Python module details when quitting
  • Better error handling: More robust HTTP request handling with better control over responses
  • IPv6 support: Added in Python 3.8 for the --bind option
  • Enhanced security: Updated security features and better default behaviors

Common Use Cases

The http.server module is perfect for various scenarios:

  1. Local development: Quickly serve static files for web development
  2. File sharing: Share files across a local network
  3. Testing: Test web applications locally without a full web server
  4. Documentation: Serve local documentation files
  5. Prototyping: Quickly prototype web applications

Sources

  1. What is the Python 3 equivalent of “python -m SimpleHTTPServer” - Stack Overflow
  2. The Python 3 Equivalent of SimpleHTTPServer - Stack Abuse
  3. What Is the Python 3 Equivalent of “Python -M Simplehttpserver” | Better Stack Community
  4. What is the Python 3 equivalent of “python -m SimpleHTTPServer”? - TechGrind
  5. How to Create a Simple HTTP Server in Python | DigitalOcean
  6. SimpleHTTPServer — Simple HTTP request handler — Python 2.7.18 documentation
  7. What is the Python 3 equivalent of “python -m SimpleHTTPServer” - PythonHow
  8. How to Launch an HTTP Server in One Line of Python Code – Real Python
  9. One-Line Python HTTP Server: Quick Start Guide | Better Stack Community
  10. Python’s http.server module - Python Morsels

Conclusion

The Python 3 equivalent of python -m SimpleHTTPServer is python3 -m http.server, which provides the same core functionality with several improvements and additional features. Key takeaways include:

  • Use python3 -m http.server for basic file serving
  • Explore command-line options like -b for binding to specific addresses and -d for serving custom directories
  • Consider creating custom handlers for advanced functionality like POST request handling
  • The module is perfect for local development, file sharing, and quick prototyping

For most use cases, the simple command-line interface will suffice, but when you need more control, the programmatic API offers extensive customization options. The transition from SimpleHTTPServer to http.server is straightforward, with most existing workflows requiring only minor adjustments to work seamlessly with Python 3.