Add web.py

This commit is contained in:
maru21 2024-11-08 17:11:35 +01:00
parent d28ec05858
commit bd28b5b2f1

80
web.py Normal file
View File

@ -0,0 +1,80 @@
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 = '''<html>
<head>
<title>PDF Files</title>
<style>
body { font-family: Arial, sans-serif; background-color: #121212; color: #e0e0e0; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
.container { max-width: 600px; background: #1e1e1e; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.5); border-radius: 8px; padding: 20px; text-align: center; }
h2 { color: #bb86fc; font-size: 24px; margin-bottom: 20px; }
ul { list-style-type: none; padding: 0; }
li { margin: 10px 0; }
a { text-decoration: none; color: #03dac6; font-weight: bold; padding: 10px 15px; border-radius: 5px; transition: background-color 0.2s ease; }
a:hover { background-color: #333; color: #bb86fc; }
.footer { margin-top: 20px; font-size: 12px; color: #666; }
</style>
</head>
<body>
<div class="container">
<h2>Available PDF Files for Download</h2>
<ul>'''
for name in file_list:
html += f'<li><a href="{name}" download>{name}</a></li>'
html += '''</ul>
<div class="footer">Serving files locally</div>
</div>
</body>
</html>'''
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 = '/path/to/your/directory'
server = PDFServer(directory, port=8000)
server.start_server()