What is the Python 3 equivalent of "python -m SimpleHTTPServer"?
In Python 2, a quick way to spin up a local HTTP server was to run:
python -m SimpleHTTPServer
This command was incredibly handy for testing or quickly sharing files within a local environment. However, if you’ve switched to Python 3, you might have noticed that SimpleHTTPServer
is no longer available by that name. Instead, Python 3 uses the http.server
module for the same purpose.
Below, you’ll find everything you need to know about how to set up and customize this lightweight HTTP server in Python 3, along with a few pro tips on Python learning resources.
1. The Simple Command
To replicate python -m SimpleHTTPServer
in Python 3, simply open your terminal or command prompt in the directory you want to serve, and run:
python3 -m http.server
- By default, this command starts serving on port 8000.
- To choose a custom port, append the port number, for example:
python3 -m http.server 8080
- Then open your browser at
http://localhost:8080
(or whichever port you used) to see the directory contents.
2. Why the Change?
- Python 2 Deprecation: Python 2 has reached end of life, meaning it no longer receives official support or updates.
- Consolidation in Python 3: The
SimpleHTTPServer
functionality has been merged under thehttp.server
module in Python 3 to streamline the standard library.
3. Customizing the Python 3 HTTP Server
Although it’s commonly used as a quick test server, you can customize the http.server
module to do more than just serve static files:
- Basic Auth: Extend
BaseHTTPRequestHandler
to add simple authentication for local testing. - Logging: Override logging behavior or integrate with Python’s logging module for debugging.
- Serve HTTPS: Use
ssl
wrapping for local HTTPS testing (handy for verifying secure connections).
Here’s a simple example that demonstrates how you could integrate HTTPS. Keep in mind this is primarily for local development purposes only:
import http.server import ssl PORT = 4443 httpd = http.server.HTTPServer(('localhost', PORT), http.server.SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket(httpd.socket, certfile='/path/to/cert.pem', # Provide your certificate file path keyfile='/path/to/key.pem', # Provide your private key file path server_side=True) print(f"Serving on https://localhost:{PORT}") httpd.serve_forever()
4. Tips for Mastering Python
Python 3’s http.server
is just one feature in a vast ecosystem. If you’re eager to sharpen your Python skills, here are a couple of excellent resources from DesignGurus.io:
- Grokking Python Fundamentals: A perfect course for beginners and intermediate developers looking to master Python 3—covering everything from syntax to advanced functionalities.
- Grokking the Coding Interview: Patterns for Coding Questions: Want to ace that next coding interview using Python? This course focuses on teaching you the most common patterns found in coding interviews, ensuring you’re prepared for a variety of challenges.
5. Leveling Up Beyond Code
If you’re planning to go beyond simple local servers and venture into scalable architectures, distributed systems, or want to prepare for tech interviews at top companies, consider exploring these additional resources:
- Grokking System Design Fundamentals – Perfect for beginners aiming to understand the foundational concepts of system design.
- Grokking the System Design Interview – A must-have if you’re preparing for a system design interview at a big tech company.
- DesignGurus YouTube Channel – Watch free videos on coding and system design, including:
6. Final Thoughts
The Python 3 equivalent of python -m SimpleHTTPServer
is python3 -m http.server
, offering the same convenient way to serve files locally. Beyond this simple command, Python’s http.server
is versatile, allowing for easy customizations and integrations with SSL for HTTPS.
As you continue your Python journey, remember that hands-on experimentation is key. Combine your newfound knowledge of local servers with structured learning resources—like the Python and system design courses from DesignGurus.io—and you’ll be well on your way to becoming a proficient, well-rounded developer.
Happy coding!