5.2.5 draco.response -- the Response object

The Response object is concerned with the response that is sent to the remote client.

The global instance of the Response class is stored under the name response in the current module. The following code fragment illustrates this:

from draco.response import response

class Response( )
Global Draco object that contains everything related to the http response.

The public methods of Response are:

setBuffering( buffer)
Enable response buffering if buffer is nonzero, or disable it otherwise. If response buffering is enabled, any data written with the write() method is added to an internal buffer, rather than written directly to the client. At the end of the http request, the Draco handler will flush this buffer to the client.

The result of a Draco template is always buffered. You only need to change this setting when you generate content manually from a Draco handler. In this case, buffering is disabled by default.

setEncoding( encoding)
Set the response encoding to encoding. The encoding parameter must be a name of an installed Python encoding. How the encoding is done depends on the type of the data written to the remote client. If this data is a regular string, it is assumed to be already in this encoding and no processing is done. If the response data is a unicode string (e.g. when using templates in unicode), it is encoded using encoding. In both cases, the encoding is communicated to the client via the "charset"parameter of the "Content-Type" http header. By default, "iso-8859-1" is used for regular strings and "utf-8" for unicode. The default encoding can be overridden in the Draco config file.

setCompression( compress)
Enable transparent compression if compress is nonzero, or disable it otherwise. Draco can transparently compress a response of content-type "text/*" if the remote client supports it. Compression is enabled by default but this can be overridden in the config file. Compression requires response buffering.

setContentType( ctype)
Set the content-type of the response to ctype. If no content-type is specified, "text/html" is assumed.

setHeader( name, value)
Set the http header name to value. Any existing http header with the same name is removed first.

addHeader( name, value)
Add an http header name with value value.

setModified( date)
The requested resource was modified at date and time date. The argument date must be a DateTime from draco.date. This method generates a "Last-Modified" http header.

When this method is called multiple times, the most recent modification date is kept. This is handy if a resource depends on multiple data sources. You can call setModified() with each of their modification dates.

write( buffer)
Write the string buffer to the remote client. Depending on whether response buffering is enabled, the data will or will not be buffered. The buffer argument can be a normal or a unicode string. If it is a normal string, it will not be processed. If it is a unicode string, it will be encoded using the response encoding.

After calling the write() method, you may not change the buffering mode. Headers may still be added if the response is buffered. If the response is not buffered, the http header is sent on the first invocation of this method. Headers that are added after this are ignored.

Note: The write() method is not needed if you are using templates and is useful only if you are manually generating content in a Draco handler. This can be for example when you are generating an image file on the fly or pushing out a file from a database.

redirect( path[, protocol=None][, host=None])
Redirect the remote client. If only the parameter path is given, the client is redirected to the current host with the current protocol. The path argument can be either an absolute path name or a path name that is relative to the current request. If protocol is specified, that protocol is used. If host is specified, redirect to that host.

The redirect is performed immediately using an exception. This method does not return!

exit( code)
Finish the request and send back http exit code code. The response is performed immediately using an exception. This method does not return!