Once streaming works without compression, enable compression by specifying Content-Encoding: deflate, and compress the HTTP response body in the ZLIB format (RFC 1950, also called as /FlateEncode). Please note that there is a similar format named GZIP (which is emitted by e.g. Java's GZIPOutputStream), but don't use that, because that produces an output which is about 10 bytes larger than for ZLIB. In Python, zlib.compress(body) can be used. In Java, use the DeflaterOutputStream or the Deflater class. You have to make more changes to make the compressor flush its output. In Python, you have to use the flush method on zlib.compressobj(). Call the .flush(1) method, where 1 corresponds to Z_PARTIAL_FLUSH (see more here). Z_SYNC_FLUSH also works, but it makes the output size larger than with Z_PARTIAL_FLUSH. (Even Z_PARTIAL_FLUSH makes the output larger than without flushing, because it forcibly closes the Huffman block, and starting a new Huffman block requires some extra bytes which describe the Huffman table of the new block.) In Java, you need at least Java 1.7 (because Java 1.6 and earlier doesn't have flushing capabilities in Deflater). If you use DeflaterOutputStream, specify the flushing mode (true for Z_SYNC_FLUSH, false is useless) as the 2nd argument of the constructor, and then call its .flush() method when you want to propagate the HTTP response accumulated so far to the web browser.
Don't send a compressed HTTP response body if the HTTP request headers don't contain Accept-Encoding with an item deflate.