Your snakelet gets the current request and response objects in the serve method, and you can do lots of stuff with them:
Scope: request
| Method | description |
|---|---|
| getOutput() | get the response output stream (socket/file). Notice that once you obtain the output stream, a number of methods will no longer work on the response object (more precisely: the methods that modify the HTTP headers that will be returned). So you have to take care of those first, before getting the output stream (try to call getOutput() as late as possible). The things you can no longer do after calling getOutput() include the following: setResponse, setHeader, setEncoding, setContentType, setContentDisposition, setContentLength, setCookie, delCookie. |
| setContentType(type) | set the content type (mime type) of the response (default is "text/html") |
| setContentDisposition(disposition) | set the content disposition (RFC 2183) of the response; you can control downloads with this. For example: setContentDisposition('attachment; filename="foobar.txt"') |
| setContentLength(len) | set the content length (number of bytes of the response data). This is useful if you know the exact size of your response in advance. Note: you cannot use this together with a custom content character encoding! If you are 100% certain that you are providing the correct length in bytes (after the character encoding has been applied!) you can give a second parameter, "force=True" to set the length even if a custom encoding has been set. Note that the server often determines the correct content-length by itself. Only in the case of snakelets it's left to you. |
| setEncoding(enc) | set character encoding of the response ('UTF-8' etc). You usually are required to set a specific character encoding if your page contains characters that are not in the plain ASCII character set. Notice that you must call getOutput() after this method! Set the encoding first, and get the output stream object after that. Also, you cannot use setContentLength() anymore. |
| guessMimeType(filename) | possible mime type for this file (string) |
| setHeader(header, value) | set a custom HTTP header |
| getHeader(header) | get a custom HTTP header previously set by setHeader, returns None if header wasn't set |
| setResponse(code, msg="output follows") | set the HTTP response code (int) and response message (string) |
| HTTPredirect(URL) | send a HTTP 302 client-side redirect to the specified URL The URL can be absolute ("http://.../...") or relative to this host ("/.../...") server-side redirection/inclusion is done on the Snakelet interface. |
| sendError(code, message=None) | send HTTP error with specified HTTP error code (int) and message (string) |
| getCookies() | return cookies set for sending (Cookie.SimpleCookie object) |
| setCookie(name, value, path=None, domain=None, maxAge=None, comment=None, secure=None) | Add a cookie that will be sent to the client. Be careful what you provide as path, when you don't specify it, a default path will be used, that can be quite different from what you expect (especially when you use internal page redirection). |
| delCookie(name, path=None, domain=None, comment=None, secure=None) | Make the client browser delete the specified cookie. Be careful what you provide as path, if it isn't exactly the same as when you originally set the cookie, it won't be deleted! |
| kill() | tries to abort the response connection. |