This is especially useful for testing purposes or when you need to quickly share some data with someone else over the network.
To launch an HTTP server in one line of Python code, simply run:
# This script is used to launch an HTTP server in one line of Python code, making it useful for testing purposes or quickly sharing data over a network.
# Import the necessary modules
import http.server # Module for creating HTTP servers
from urllib.parse import urlparse # Module for parsing URLs
import shutil # Module for high-level file operations
# Set the port and host for the server
PORT = 8000
HOST = '127.0.0.1'
# Create a class for handling HTTP requests
class Handler(http.server.SimpleHTTPRequestHandler):
# Define a method for handling GET requests
def do_GET(self):
# Parse the requested file path from the URL
file_path = urlparse(self.path).path[1:] if len(urlparse(self.path).path) > 1 else 'index.html'
# Open the requested file in the "downloads" directory
with open('downloads/' + file_path, 'rb') as f:
# Send a 200 OK response
self.send_response(200)
# Set the content type to "application/octet-stream"
self.send_header("Content-type", "application/octet-stream")
# End the headers
self.end_headers()
# Copy the file object to the response body
shutil.copyfileobj(f, self.wfile)
# Close the file
f.close()
# Create an HTTP server with the specified host, port, and handler
http.server.HTTPServer((HOST, PORT), Handler).serve_forever()
This code defines a custom HTTP handler that serves static files from the “downloads” directory. The `urlparse` function is used to extract the requested file path and handle cases where no specific file was requested (i.e., just “/”). Note that we’re using Python’s built-in shutil module to copy the contents of the opened file object to the server response stream, which allows us to avoid reading the entire file into memory at once.
To run this code, save it as a .py script and execute it from your terminal or command prompt:
#!/bin/bash
# This is a bash script that will run a Python http server.
# It is important to include the shebang at the beginning of the script to specify the interpreter to use.
python http_server.py # This line executes the Python script named "http_server.py".
# To run this script, save it as a .sh file and execute it from your terminal or command prompt using the command "bash script_name.sh".
# Note: It is important to have the correct file permissions set in order to execute the script.
# The following code uses the built-in "shutil" module from Python to copy the contents of the opened file object to the server response stream.
# This allows us to avoid reading the entire file into memory at once, which can be more efficient for larger files.
# To use the "shutil" module, we need to import it first. This is done in the Python script, so we do not need to do it here.
# The "python" command is used to execute the Python script, followed by the name of the script.
# It is good practice to include comments throughout the script to explain the purpose and functionality of each code segment. This makes the code easier to understand and maintain.
This will launch an HTTP server on port 8000 that serves static files from the “downloads” directory. You can access this server by opening a web browser and navigating to `http://127.0.0.1:8000`. Note that you’ll need to replace ‘127.0.0.1’ with your local IP address if you want to share the files over the network.
This is just a basic example, but you can customize this code to suit your needs by modifying the `Handler` class and its methods as needed. For more information on how to use Python’s built-in HTTP server, check out the official documentation: https://docs.python.org/3/library/http.server.html
In this tutorial, you learned how to launch an HTTP server in one line of Python code using a custom handler that serves static files from a directory. You also saw some tips for modifying and customizing this code to suit your needs.