Discover the Python 3 equivalent of "python -m SimpleHTTPServer" for effortlessly running a basic web server from your command line.
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.
The Python 3 equivalent of the command python -m SimpleHTTPServer
is python3 -m http.server
.
Here's why:
SimpleHTTPServer
module was used in Python 2 to create a simple web server.SimpleHTTPServer
was merged into the http.server
module.To use it:
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:
python3 -m http.server <port_number>
(replace <port_number>
with your desired port).Important Notes:
sudo
) to run the server on ports below 1024.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:
Import necessary modules:
http.server
provides the functionality for the web server.socketserver
helps manage the underlying network socket.Set the port:
PORT = 8000
defines the port the server will listen on.Create a request handler:
Handler = http.server.SimpleHTTPRequestHandler
uses the default request handler for serving files.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:
myserver.py
).python3 myserver.py
.This will start a simple web server on port 8000, just like the command-line example.
python3 -m http.server 8080 index.html
will serve index.html
when you access http://localhost:8080/
.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
or python3
) based on your system's default Python version or virtual environment setup.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.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:
SimpleHTTPServer
module in Python 2 was integrated into the http.server
module in Python 3.Important Considerations:
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.
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...