Created
February 14, 2024 18:17
-
-
Save CodeByAidan/8b8184f3d023ad3b03ff864340ceca14 to your computer and use it in GitHub Desktop.
A simple HTTP server that serves a single PDF file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A simple HTTP server that serves a single PDF file. | |
This script creates a basic HTTP server that serves a single PDF file to clients. | |
The server listens on port 8000 by default and serves the specified PDF file. | |
Clients can access the PDF file by visiting the server's IP address. | |
Usage: | |
python3 server.py | |
""" | |
import socket | |
import socketserver | |
from http.server import SimpleHTTPRequestHandler | |
PORT: int = 8000 | |
PDF_FILE: str = "example.pdf" # Replace 'example.pdf' with the path to your PDF file | |
class PDFHandler(SimpleHTTPRequestHandler): | |
""" | |
Custom handler to serve only the PDF file | |
Args: | |
SimpleHTTPRequestHandler: Base class for handling HTTP requests | |
""" | |
def __init__(self: "PDFHandler", *args: str, **kwargs: str) -> None: | |
""" | |
Initialize the PDFHandler. | |
Args: | |
*args: Variable length argument list. | |
**kwargs: Arbitrary keyword arguments. | |
""" | |
super().__init__(*args, **kwargs) | |
self.path: str = PDF_FILE | |
def end_headers(self: "PDFHandler") -> None: | |
""" | |
Add Content-Disposition header for inline PDF viewing. | |
""" | |
self.send_header("Content-Disposition", f'inline; filename="{PDF_FILE}"') | |
super().end_headers() | |
def do_GET(self: "PDFHandler") -> None: | |
""" | |
Handle GET request. | |
""" | |
if self.path == "/": | |
self.path = PDF_FILE | |
super().do_GET() | |
# Get the local IP address | |
def get_ip() -> str: | |
""" | |
Get the local IP address. | |
Returns: | |
str: Local IP address. | |
""" | |
return socket.gethostbyname(socket.gethostname()) | |
def serve() -> None: | |
""" | |
Start the HTTP server. | |
""" | |
with socketserver.TCPServer(("", PORT), PDFHandler) as httpd: | |
print(f"Serving at IP: {get_ip()}, port: {PORT}") | |
httpd.serve_forever() | |
if __name__ == "__main__": | |
serve() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment