How to Mock a Chunked Encoding Streaming Response in Python
Build a local mock HTTP server with Python's http.server that streams a chunked-encoded response with a 0.5s delay per chunk.
Python code
37 linesfrom http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import time
class ChunkedHandler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
def do_GET(self):
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.send_header("Transfer-Encoding", "chunked")
self.end_headers()
for i in range(1, 6):
chunk = f"Chunk-{i}\n".encode()
# write hex length + CRLF + data + CRLF
self.wfile.write(f"{len(chunk):X}\r\n".encode())
self.wfile.write(chunk + b"\r\n")
self.wfile.flush()
time.sleep(0.5)
# terminating chunk
self.wfile.write(b"0\r\n\r\n")
self.wfile.flush()
def log_message(self, format, *args):
pass
def main():
server = ThreadingHTTPServer(("127.0.0.1", 8080), ChunkedHandler)
print("Serving on port 8080 (Press Ctrl+C to stop)")
try:
server.serve_forever()
except KeyboardInterrupt:
server.shutdown()
if __name__ == "__main__":
main()
Output
Serving on port 8080 (Press Ctrl+C to stop)
When a client connects (e.g., with curl):
HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked
Chunk-1
Chunk-2
Chunk-3
Chunk-4
Chunk-5
Each chunk appears one every 0.5 seconds.
How it works
The handler sets protocol_version = "HTTP/1.1" so connections stay open and support chunked transfer. send_response(200) sends the status line and headers; then end_headers() finalizes the header block. Each chunk is encoded to bytes, and its hex length is written first, followed by CRLF, the chunk data, and another CRLF as per the HTTP/1.1 chunked transfer protocol. Flushing after each write ensures data is pushed to the client immediately, and sleeping simulates streaming latency. The terminating 0\r\n\r\n chunk signals the end of the stream.
Common mistakes
- Forgetting to set `protocol_version = "HTTP/1.1"` — HTTP/1.0 does not support chunked encoding.
- Omitting the CRLF after the chunk length or between chunks, breaking the protocol.
- Not flushing after each write may buffer all data and defeat streaming.
- Sending the terminating chunk as `0\r\n` only — you must include the trailing CRLF.
Variations
- Use `http.server.HTTPServer` instead of a threaded server for simpler single-client testing.
- Stream binary data (e.g., JSON chunks) by encoding each chunk with `json.dumps`.
Real-world use cases
- Locally testing a client that consumes an LLM's token-by-token streaming API without hitting a paid service.
- Verifying that a web client handles incremental data rendering in server-sent events or chunked download scenarios.
- Prototyping an API endpoint that requires transfer-encoding chunked for real-time data feeds like stock tickers or chat messages.
Sponsored
More from API design & gRPC
- Build a Bulk Array POST Mock Server in Python medium
- Build a Mock REST API with PUT and GET in Python medium
- Convert Protobuf to JSON and Dict in Python easy
- Create a Data Helper in Python for gRPC-style APIs easy
- Format data in Python using dataclasses like gRPC messages easy
- Generate an OpenAPI Spec from Mock Routes in Python easy
Keep learning
Related tutorials and quizzes for this topic.