Snakelet class

Your own snakelets have to subclass from the generic Snakelet class and thus inherit all methods from that class. You can use the following methods of the Snakelet base class:

Method description
serve(request, response) must override this to handle requests, the parameters are the request and response objects.
getWebApp() the current WebApp object. Contains additional interesting methods.
getContext() snakelet context (ContextContainer object). Scope: snakelet. shared for all users/requests
getAppContext() webapp context (ContextContainer object). Scope: web app. shared for all users/requests
getPlugin(pluginname) look up the (running) plugin object by its name (raises KeyError if not found)
getPluginNames() a list of names of the installed plugins
getFullURL() returns full URL of this snakelet (without query args). if you are using URL patterns, this cannot be used in that snakelet! (because the pattern ends up in the URL) Example: "http://desertfish.xs4all.nl:9080/test/snoop.sn" NOTE:The hostname in the url is the virtual-host-name from what Snakelets thinks is the vhost for this request. If you are running Snakelets behind a proxy (and/or the bindname is different), this hostname may not be correct! The solution is to enable the Vhost config and to define a correct virtual-host mapping for the 'real' hostname. You can still use a different bindname, if you wish.
getURL() server-relative URL pattern of this snakelet or Ypage, example: "/test/snoop.sn" If you use a URL pattern for your snakelet, that pattern is returned (example: "/test/*.pdf")! So it is not safe to use this method for constructing URLs. Use the getRequestURLplain method of the request object instead! If you are not using URL patterns, you're safe.
For Ypages with a form this is useful if the form should be posted to the page itself, you can then easily do <form action="<%=self.getURL()%>" .... >
init() override this to perform custom initialization at load time
getDescription() override to return a description string for the snakelet
requiresSession() override this to return one of the special SESSION_* values defined in the Snakelet class (for instance return self.SESSION_WANTED), that signify what kind of session is required. Also see the session Ypage-declaration below. The possible values are: SESSION_NOT_NEEDED (no session and session cookie), SESSION_WANTED (use session and session cookie), SESSION_REQUIRED (requires a synchronised session), SESSION_LOGIN_REQUIRED (requires a session with a logged in user), SESSION_DONTCREATE (use existing session, if available, if not: do NOT create a new session). If you use the last one and you do not provide a means of user-authentication (see getAuthMethod below), Snakelets will return a 403-error page when you try to access the page if you are not logged in.
getAuthorizedRoles() override this to return a list or set of privilige names that are allowed to access this snakelet. Default is None (no access control).
getAuthMethod() override this to return a tuple (method,argument) that defines the authentication method to use for this snakelet. See authorization.
allowCaching() override this to return True if the browser is allowed to cache the page, False otherwise. Default is False.
escape(str) escapes a string to make it HTML-safe (< becomes &lt; etc)
urlescape(str) escapes a string to make it URL-safe (spaces become '+', ? and & are translated etc). Don't use this on full URLs with URL query args, because it escapes the characters that must be there to separate the args. It is intended for the value strings for those args.
urlunescape(str) un-escapes an url-escaped string. Only use this for URL component strings, not on whole URLs!
redirect(URL, request, response) internal redirect to anoter URL ( "http://..../.." or "/webapp/page.y" ) Note: new URL query args are NOT parsed!
include(URL, request, response) internal include of another URL ( "http://..../..." or "/webapp/page.y" ) Note: new URL query args are NOT parsed! Note: the current implementation is broken and doesn't process multi-level includes correctly. Also the content-type of the page will be wrong when using page include.
getErrorPage() the URL of the currently defined custom error page, or None.
setErrorPage(URL) set a custom error page (URL string) that must be shown -instead of the faulty page- when an unhandled exception occurs in this snakelet. See the "errorpage" declaration for more details on the data available to you in your custom error page.

Getting the URL prefixes, easy URL creation

In Ypages you have some shortcuts to easily obtain the current URL prefix and so on. From a Snakelet, you can do exactly that but not directly: you have to get the webapp first using getWebApp() and use its methods instead. The webapp object also has two convenient methods to create urls: mkUrl and mkAssetUrl!

Using self in Snakelets to store data

Short: you shouldn't. Long: your Snakelet code has no single, local, 'personal', environment it is running in. There is only one object (instance) of your Snakelet and it might be accessed concurrently by multiple threads. Only use local objects to store temporary data in, never create new properties on self! If you really need to store some data that is local for this Snakelet, and shared for all users and all requests, you can use the getContext() method to obtain the local Snakelet data context.

Making a download snakelet that serves static file content

Use self.getWebApp().serveStaticFile(filename, response, useResponseHeaders=False) to utilize the server's own internal code to serve static files efficiently. See the webapp chapter.

Snakelets manual - Back to index