import os from http.server import HTTPServer, SimpleHTTPRequestHandler from io import BytesIO class PDFServer: def __init__(self, directory, port=8000): """Initialize the server with a directory to serve PDF files and a specified port.""" self.directory = directory self.port = port def start_server(self): """Start the HTTP server and serve PDF files.""" os.chdir(self.directory) # Change to the specified directory server_address = ('', self.port) httpd = HTTPServer(server_address, self.PDFRequestHandler) print(f"Serving PDF files on http://localhost:{self.port}") httpd.serve_forever() class PDFRequestHandler(SimpleHTTPRequestHandler): """Custom request handler to display a list of PDF files with dark material styling.""" def list_directory(self, path): """Override the directory listing method to only show PDF files with custom styling.""" try: file_list = os.listdir(path) except os.error: self.send_error(404, "No permission to list directory") return None file_list = [f for f in file_list if f.endswith('.pdf')] # Filter only PDF files file_list.sort(key=lambda a: a.lower()) # HTML content with dark and blue-themed Material Design styling html_content = self.generate_html(file_list) return self.send_html_response(html_content) def generate_html(self, file_list): """Generate HTML content for displaying the PDF files.""" html = ''' PDF Files

Available PDF Files for Download

''' return html def send_html_response(self, html_content): """Send an HTML response to the client.""" f = BytesIO() f.write(html_content.encode('utf-8')) f.seek(0) self.send_response(200) self.send_header("Content-type", "text/html; charset=utf-8") self.send_header("Content-Length", str(len(f.getvalue()))) self.end_headers() return f # Usage if __name__ == "__main__": directory = '/scrapy/amznMailConverter/data' server = PDFServer(directory, port=8000) server.start_server()