The Response Object

You can use the HttpResponse object to send data to the webbrowser.

The very basic usage is explained here:

from colubrid import BaseApplication, HttpResponse

class MyApplication(BaseApplication):

    def process_request(self):
        return HttpResponse('Hello World')

Constructor

The constructor takes three parameters. The first one is either a string (unicode objects are converted to strings on the fly) or an iterable.

The second parameter can either be a list of headers or an HttpHeaders object.

The last one is the status code which defaults to 200.

Here an example plain/text error404 response:

error = HttpResponse('page not found', [('Content-Type', 'text/plain')], 404)

Modifying headers

To add response headers, you can use the mapping protocol on either response.headers or response:

response.headers['Content-Type'] = 'text/plain'
response['Content-Length'] = 2048

This also works for getting and removing header values:

content = response.headers.get('Content-Type')
length = response['Content-Length']

del response.headers['Content-Type']
del response['Content-Length']

To add an additional header for a key, you can use response.headers.add:

response.headers.add('Set-Cookie', 'name=value')
response.headers.add('Set-Cookie', 'name2=value')

write

You can use response.write(data) to send a string or unicode object into the buffer. This only works if the response object was instanced using a string, unicode or list object.

Cookie management

You can use response.set_cookie to set or modify a cookie. The parameters are the same as in the cookie Morsel object in the Python standard library.

Example:

response.set_cookie('username', 'myname', domain='.mydomain.com')

To remove existing cookies, use response.delete_cookie:

response.delete_cookie('username')
Last Change: 10 Sep 2006 17:12:27 | show source