cherrypy.lib.encoding module

class cherrypy.lib.encoding.ResponseEncoder(**kwargs)[source]

Bases: object

add_charset = True
debug = False
default_encoding = 'utf-8'

Encode a streaming response body.

Use a generator wrapper, and just pray it works as the stream is being written out.


Encode a buffered response body.

encoding = None
errors = 'strict'
failmsg = 'Response body could not be encoded with %r.'
text_only = True
class cherrypy.lib.encoding.UTF8StreamEncoder(iterator)[source]

Bases: object

cherrypy.lib.encoding.compress(body, compress_level)[source]

Compress ‘body’ at the given compress_level.

cherrypy.lib.encoding.decode(encoding=None, default_encoding='utf-8')[source]

Replace or extend the list of charsets used to decode a request entity.

Either argument may be a single string or a list of strings.


If not None, restricts the set of charsets attempted while decoding a request entity to the given set (even if a different charset is given in the Content-Type request header).


Only in effect if the ‘encoding’ argument is not given. If given, the set of charsets attempted while decoding a request entity is extended with the given value(s).

cherrypy.lib.encoding.gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False)[source]

Try to gzip the response body if Content-Type in mime_types.

cherrypy.response.headers[‘Content-Type’] must be set to one of the values in the mime_types arg before calling this function.

The provided list of mime-types must be of one of the following form:
  • type/subtype

  • type/*

  • type/*+subtype

No compression is performed if any of the following hold:
  • The client sends no Accept-Encoding request header

  • No ‘gzip’ or ‘x-gzip’ is present in the Accept-Encoding header

  • No ‘gzip’ or ‘x-gzip’ with a qvalue > 0 is present

  • The ‘identity’ value is given with a qvalue > 0.


Ensure response body is iterable and resolves to False when empty.