🐶
Python

Python 3 SimpleHTTPServer Equivalent: http.server

By Filip on 10/05/2024

Discover the Python 3 equivalent of "python -m SimpleHTTPServer" for effortlessly running a basic web server from your command line.

Python 3 SimpleHTTPServer Equivalent: http.server

Table of Contents

Introduction

In Python 2, you could quickly start a simple web server using the command python -m SimpleHTTPServer. However, this command has changed in Python 3.

Step-by-Step Guide

The Python 3 equivalent of the command python -m SimpleHTTPServer is python3 -m http.server.

Here's why:

  • Python 2 Legacy: The SimpleHTTPServer module was used in Python 2 to create a simple web server.
  • Python 3 Update: In Python 3, SimpleHTTPServer was merged into the http.server module.

To use it:

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to serve files from.
  3. Run the command: python3 -m http.server (or python -m http.server if your system defaults to Python 3).

This will start a server on port 8000 by default. You can then access your files in a web browser by going to http://localhost:8000/.

To specify a different port:

  • Use the command: python3 -m http.server <port_number> (replace <port_number> with your desired port).

Important Notes:

  • Permissions: On some systems, you might need root privileges (using sudo) to run the server on ports below 1024.
  • Security: This server is intended for simple use cases and development purposes. It's not recommended for production environments due to security limitations.

Code Example

This Python code creates a simple web server that serves files from the current directory. It uses the http.server and socketserver modules to handle HTTP requests and manage the server socket. The server listens on port 8000 and runs until manually stopped.

This article doesn't require a code example. It explains how to use a command-line tool, not how to write Python code.

However, if you'd like to achieve similar functionality within a Python script, here's how you can do it:

import http.server
import socketserver

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print(f"Serving at port {PORT}")
    httpd.serve_forever()

Explanation:

  1. Import necessary modules:

    • http.server provides the functionality for the web server.
    • socketserver helps manage the underlying network socket.
  2. Set the port:

    • PORT = 8000 defines the port the server will listen on.
  3. Create a request handler:

    • Handler = http.server.SimpleHTTPRequestHandler uses the default request handler for serving files.
  4. Start the server:

    • with socketserver.TCPServer(("", PORT), Handler) as httpd: creates and starts the server on the specified port.
    • print(f"Serving at port {PORT}") displays a message indicating the server is running.
    • httpd.serve_forever() keeps the server running until manually stopped.

To run this code:

  1. Save it as a Python file (e.g., myserver.py).
  2. Open your terminal and navigate to the directory where you saved the file.
  3. Run the script using python3 myserver.py.

This will start a simple web server on port 8000, just like the command-line example.

Additional Notes

  • Directory Listing: By default, the server displays a directory listing of the files in the specified directory. This allows you to browse and download files directly from your web browser.
  • Serving a Specific File: You can directly serve a specific file by providing its path after the port number in the command. For example: python3 -m http.server 8080 index.html will serve index.html when you access http://localhost:8080/.
  • Stopping the Server: To stop the server, simply press Ctrl+C in your terminal.
  • Alternatives: While http.server is convenient for simple use cases, for more advanced features or production environments, consider using more robust web frameworks like Flask or Django.
  • Python Version Compatibility: Always ensure you are using the correct command (python or python3) based on your system's default Python version or virtual environment setup.
  • Error Handling: The basic server doesn't have robust error handling. For example, if a requested file is not found, it will return a simple error message in the browser.
  • Customization: While the default SimpleHTTPRequestHandler is sufficient for basic file serving, you can customize the server's behavior by subclassing http.server.BaseHTTPRequestHandler and overriding methods to handle specific HTTP requests or add custom logic.

Summary

Feature Python 2 Python 3
Module SimpleHTTPServer http.server
Command python -m SimpleHTTPServer python3 -m http.server
Default Port 8000 8000
Port Specification Not mentioned python3 -m http.server <port_number>

Key Changes:

  • The SimpleHTTPServer module in Python 2 was integrated into the http.server module in Python 3.
  • Specifying a custom port is now supported in Python 3.

Important Considerations:

  • Running the server on ports below 1024 might require root privileges.
  • This server is not suitable for production environments due to security limitations.

Conclusion

In conclusion, while Python 2 used the python -m SimpleHTTPServer command to create a simple web server, Python 3 has transitioned to using python3 -m http.server. This change reflects the merging of the SimpleHTTPServer module into http.server. Despite this change, the core functionality remains the same, allowing developers to quickly serve files from a directory. However, it's crucial to remember that this built-in server is primarily for development and testing due to security limitations. For production environments, exploring robust frameworks like Flask or Django is recommended.

References

According to all the X3DOM literature this should be a simple do.

Python was installed successfully in my 'site' directory which also holds my index.html file.

Caveat: I do not wish to become a server SW engineer, I jus...

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait