Last active
June 1, 2026 13:11
-
-
Save arantius/a00eef526ce468c5b14157f05d43f30f to your computer and use it in GitHub Desktop.
A simple tool to serve slow HTTP resposes. So the start and end time will be different.
This file contains hidden or 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
| import http.server | |
| import time | |
| import socketserver | |
| from urllib.parse import urlparse, parse_qs | |
| # Configuration constants | |
| DEFAULT_NUM_PARAGRAPHS = 5 | |
| DEFAULT_DELAY_SECONDS = 0.5 | |
| class ThreadedHTTPServer(socketserver.ThreadingMixIn, http.server.HTTPServer): | |
| """Handle requests in a separate thread.""" | |
| pass | |
| class StreamingHandler(http.server.BaseHTTPRequestHandler): | |
| def do_GET(self): | |
| # Parse path and query parameters | |
| parsed_url = urlparse(self.path) | |
| params = parse_qs(parsed_url.query) | |
| # Extract parameters or use defaults | |
| try: | |
| num_paragraphs = int(params.get("paragraphs", [DEFAULT_NUM_PARAGRAPHS])[0]) | |
| except (ValueError, IndexError): | |
| num_paragraphs = DEFAULT_NUM_PARAGRAPHS | |
| try: | |
| delay_seconds = float(params.get("delay", [DEFAULT_DELAY_SECONDS])[0]) | |
| except (ValueError, IndexError): | |
| delay_seconds = DEFAULT_DELAY_SECONDS | |
| # Send initial headers | |
| self.send_response(200) | |
| self.send_header("Content-Type", "text/html; charset=utf-8") | |
| self.send_header("Connection", "close") | |
| self.end_headers() | |
| try: | |
| self.wfile.write(b"<!DOCTYPE html><html><body>\n") | |
| self.wfile.flush() | |
| # Insert iframe only on the root URL path | |
| if parsed_url.path == "/": | |
| iframe_html = '<iframe src="/stream?delay=1.0" width="400" height="300"></iframe>\n' | |
| self.wfile.write(iframe_html.encode("utf-8")) | |
| self.wfile.flush() | |
| for i in range(1, num_paragraphs + 1): | |
| time.sleep(delay_seconds) | |
| html_chunk = f"<p>Path '{self.path}': Paragraph {i} of {num_paragraphs} (delay={delay_seconds}s)</p>\n" | |
| self.wfile.write(html_chunk.encode("utf-8")) | |
| self.wfile.flush() | |
| self.wfile.write(b"</body></html>\n") | |
| self.wfile.flush() | |
| except BrokenPipeError: | |
| pass | |
| if __name__ == "__main__": | |
| server_address = ("", 8080) | |
| httpd = ThreadedHTTPServer(server_address, StreamingHandler) | |
| print("Threaded server running on http://localhost:8080...") | |
| httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment