Python 3
SimpleHTTPServer
http.server
Python command line
web server

What is the Python 3 equivalent of python -m SimpleHTTPServer

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

bash
python -m SimpleHTTPServer

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:

bash
python3 -m http.server

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:

bash
python3 -m http.server 8080

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:

bash
python3 -m http.server --bind 127.0.0.1

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:

python
1import http.server
2import socketserver
3
4class MyRequestHandler(http.server.SimpleHTTPRequestHandler):
5    def do_POST(self):
6        # Custom POST handling code here
7        pass
8
9PORT = 8080
10
11with socketserver.TCPServer(("", PORT), MyRequestHandler) as httpd:
12    print(f"Serving on port {PORT}")
13    httpd.serve_forever()

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.

FeaturePython 2Python 3
Default ModuleSimpleHTTPServerhttp.server
Start Commandpython -m SimpleHTTPServerpython3 -m http.server
Default Port80008000
Different Port Usagepython -m SimpleHTTPServer 8080python3 -m http.server 8080
Binding to AddressNot supportedpython3 -m http.server --bind 127.0.0.1
Custom Request HandlerRequires 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.


Course illustration
Course illustration

All Rights Reserved.