What is the Python 3 equivalent of python -m SimpleHTTPServer
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
The transition from Python 2 to Python 3 involved several changes in syntax and functionality that affect developers in various ways. One such change involves setting up a simple HTTP server for file sharing or testing purposes. In Python 2, you could quickly start a simple HTTP server in the current directory using:
In Python 3, this module has been deprecated and replaced with http.server. Let's dive into how you can use this new module in Python 3, along with some technical insights.
Serving Files with Python 3: http.server
In Python 3, the use of SimpleHTTPServer has been simplified into the http.server module. To start a basic HTTP server, you can use the following command:
This command starts an HTTP server serving files from the current directory. By default, it serves on port 8000. Here's a breakdown of how you can customize this command to suit different needs.
Specifying a Different Port
You might want to start the server on a port other than the default 8000. To do so, you can specify a port number at the end of the command:
This command starts the server on port 8080.
Binding to a Specific Address
By default, the server listens to requests on all interfaces. To bind the server to a specific IP address, use the --bind option:
This command binds the server to the local interface 127.0.0.1, which means the server will only be accessible from the machine it’s running on.
Using a Different Handler
The http.server module allows for customization of request handling. For instance, you might want to handle POST requests differently. To achieve this, you create a subclass of http.server.SimpleHTTPRequestHandler:
By running this script, you start an HTTP server using your custom request handler.
Comparing Python 2 vs Python 3 Servers
Here’s a quick comparison table for setting up HTTP servers in Python 2 and Python 3.
| Feature | Python 2 | Python 3 |
| Default Module | SimpleHTTPServer | http.server |
| Start Command | python -m SimpleHTTPServer | python3 -m http.server |
| Default Port | 8000 | 8000 |
| Different Port Usage | python -m SimpleHTTPServer 8080 | python3 -m http.server 8080 |
| Binding to Address | Not supported | python3 -m http.server --bind 127.0.0.1 |
| Custom Request Handler | Requires external libraries or more code |
Additional Subtopics
Security Considerations
A Simple HTTP server is a quick and convenient way to share files but is not suitable for a production environment. It lacks many security features (e.g., HTTPS, authentication, protection against directory traversal attacks). For anything beyond local development and basic testing, consider using a full-fledged web server or framework like Flask or Django, which are available in Python.
Use Cases
Common scenarios to use Python's Simple HTTP Server include:
- Testing Web Pages: Quickly check the layout, scripts, or styles of your web pages without needing additional software.
- Distributing Files: Serve files on a local network for easy sharing during meetings, workshops, or collaborative tasks.
- Learning and Teaching: Help beginners understand web concepts like request handling, response codes, or directories on servers.
Conclusion
The move to http.server in Python 3 provides developers with enhanced functionality and greater flexibility. While SimpleHTTPServer was simple and effective, the new module opens the door for customization and improvements, maintaining simplicity for easy tasks while offering options for more complex needs.
Related reading
- What is the quickest way to HTTP GET in Python?
- What is the relation between docker0 and eth0?
- What is the REST or CLI API for logging in to Amazon Cognito user pools
- What is the use for CRD status?
- What is the Python equivalent for a case/switch statement?
- What is the Python equivalent for a case/switch statement?
- what is the use of external IP address with a ClusterIP service type in kubernetes
- What is VPC, Subnet in AWS

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.