Quick Http Server For File Serving

jasonr

Mon Dec 02 2024 11:14:12 AM PST

import http.server
import socketserver

def serve_files(directory=".", port=8000):
    """
    Serves files from the specified directory on an HTTP server.

    :param directory: Directory to serve files from (default is the current directory).
    :param port: Port to host the server on (default is 8000).
    """
    try:
        handler = http.server.SimpleHTTPRequestHandler
        with socketserver.TCPServer(("", port), handler) as httpd:
            print(f"Serving files from {directory} at http://:{port}")
            print("Press Ctrl+C to stop the server.")
            httpd.serve_forever()
    except KeyboardInterrupt:
        print("\nServer stopped.")
    except Exception as e:
        print(f"Error starting server: {e}")

# Example usage
if __name__ == "__main__":
    serve_files(directory="/path/to/your/files", port=8000)

This script is a Python program that sets up a simple HTTP server to serve files from a specified directory.



programming python server