What is the Python 3 equivalent of python -m SimpleHTTPServer?
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
- Command Line Options
- Advanced Features and Customization
- Programmatic Usage
- Comparison with SimpleHTTPServer
- Common Use Cases
Basic Usage
The most straightforward replacement for python -m SimpleHTTPServer in Python 3 is:
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:
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:
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:
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:
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:
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:
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.servermodule 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
--bindoption - Enhanced security: Updated security features and better default behaviors
Common Use Cases
The http.server module is perfect for various scenarios:
- Local development: Quickly serve static files for web development
- File sharing: Share files across a local network
- Testing: Test web applications locally without a full web server
- Documentation: Serve local documentation files
- Prototyping: Quickly prototype web applications
Sources
- What is the Python 3 equivalent of “python -m SimpleHTTPServer” - Stack Overflow
- The Python 3 Equivalent of SimpleHTTPServer - Stack Abuse
- What Is the Python 3 Equivalent of “Python -M Simplehttpserver” | Better Stack Community
- What is the Python 3 equivalent of “python -m SimpleHTTPServer”? - TechGrind
- How to Create a Simple HTTP Server in Python | DigitalOcean
- SimpleHTTPServer — Simple HTTP request handler — Python 2.7.18 documentation
- What is the Python 3 equivalent of “python -m SimpleHTTPServer” - PythonHow
- How to Launch an HTTP Server in One Line of Python Code – Real Python
- One-Line Python HTTP Server: Quick Start Guide | Better Stack Community
- 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.serverfor basic file serving - Explore command-line options like
-bfor binding to specific addresses and-dfor 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.