title: How to stream a gzip built on the fly in Python?
body: <p>I'd like to stream a big log file over the network using asyncio. I retrieve the data from the database, format it, compress it using python's zlib and stream it over the network.</p>
  
  <p>Here is basically the code I use:</p>
  
  <pre><code>@asyncio.coroutine
  def logs(requests):
      # ...
  
      yield from resp.prepare(request)
  
      # gzip magic number and compression format
      resp.write(b'\x1f\x8b\x08\x00\x00\x00\x00\x00')
      compressor = compressobj()
      for row in rows:
          ip, uid, date, url, answer, volume = row
          NCSA_ROW = '{} {} - [{}] "GET {} HTTP/1.0" {} {}\n'
          row = NCSA_ROW.format(ip, uid, date, url, answer, volume)
          row = row.encode('utf-8')
          data = compressor.compress(row)
          resp.write(data)
      resp.write(compressor.flush())
      return resp
  </code></pre>
  
  <p>The file that I retrieve can not be opened with gunzip and zcat raise the following error:</p>
  
  <pre><code>gzip: out.gz: unexpected end of file
  </code></pre>