spyce
         
home     documentation     download     Spyce logo


Documentation - Modules

Prev: 3.8.8 - Transform Up: 3.8 - Modules Next: 3.8.10 - Include

3.8.9. Compress

The compress module supports dynamic compression of Spyce output, and can save bandwidth in addition to static compaction. The different forms of compression supported are described below.

  • spaces( [ boolean ] ):
    Controls dynamic space compression. Dynamic space compression will eliminate consecutive whitespaces (spaces, newlines and tabs) in the output stream, each time it is flushed. The optional boolean parameter defaults to true.

  • gzip( [ level ] ):
    Applies gzip compression to the Spyce output stream, but only if the browser can support gzip content encoding. Note that this function will fail if the output stream has already been flushed, and should generally only be used with buffered output streams. The optional level parameter specifies the compression level, between 1 and 9 inclusive. A value of zero disables compression. If level is omitted, the default gzip compression level is used. This function will automatically check the request's Accept-Encoding header, and set the response's Content-Encoding header.

The example below shows the compression module in use.

examples/compress.spy
[[.import name=compress args="gzip=1, spaces=1"]]
[[\
  response.write('<html><body>')
  response.write('  Space compression will remove these     spaces.<br>')
  response.write('  gzip compression will highly compress this:<br>')
  for i in range(1000):
    response.write('  hello')
  response.write('</body></html>')
]]
Run this code

Note that the compression functions need not be called at the beginning of the input, but before the output stream is flushed. Also, to really see what is going on, you should telnet to your web server, and provide something like the following request.

GET /spyce/examples/compress.spy HTTP/1.1
Accept-Encoding: gzip


Prev: 3.8.8 - Transform Up: 3.8 - Modules Next: 3.8.10 - Include


Spyce logo
Python Server Pages
version 2.1.3
Spyce Powered SourceForge Logo