HTTPContent

Content producing HTTP servlet.

class HTTPContent.HTTPContent

Bases: HTTPServlet

Content producing HTTP servlet.

HTTPContent is a type of HTTPServlet that is more convenient for Servlets which represent content generated in response to GET and POST requests. If you are generating HTML content, you you probably want your servlet to inherit from Page, which contains many HTML-related convenience methods.

If you are generating non-HTML content, it is appropriate to inherit from this class directly.

Subclasses typically override defaultAction().

In awake, the page sets self attributes: _transaction, _response and _request which subclasses should use as appropriate.

For the purposes of output, the write and writeln convenience methods are provided.

If you plan to produce HTML content, you should start by looking at Page instead of this lower-level class.

__init__()

Subclasses must invoke super.

actions()

The allowed actions.

Returns a list or a set of method names that are allowable actions from HTML forms. The default implementation returns []. See _respond for more about actions.

application()

The Application instance we’re using.

awake(transaction)

Let servlet awake.

Makes instance variables from the transaction. This is where Page becomes unthreadsafe, as the page is tied to the transaction. This is also what allows us to implement functions like write, where you don’t need to pass in the transaction or response.

callMethodOfServlet(url, method, *args, **kwargs)

Call a method of another servlet.

See Application.callMethodOfServlet for details. The main difference is that here you don’t have to pass in the transaction as the first argument.

canBeReused()

Returns whether a single servlet instance can be reused.

The default is True, but subclasses con override to return False. Keep in mind that performance may seriously be degraded if instances can’t be reused. Also, there’s no known good reasons not to reuse an instance. Remember the awake() and sleep() methods are invoked for every transaction. But just in case, your servlet can refuse to be reused.

canBeThreaded()

Declares whether servlet can be threaded.

Returns False because of the instance variables we set up in awake.

close()
defaultAction()

Default action.

The core method that gets called as a result of requests. Subclasses should override this.

static endResponse()

End response.

When this method is called during awake or respond, servlet processing will end immediately, and the accumulated response will be sent.

Note that sleep will still be called, providing a chance to clean up or free any resources.

forward(url)

Forward request.

Forwards this request to another servlet. See Application.forward for details. The main difference is that here you don’t have to pass in the transaction as the first argument.

handleAction(action)

Handle action.

Invoked by _respond when a legitimate action has been found in a form. Invokes preAction, the actual action method and postAction.

Subclasses rarely override this method.

includeURL(url)

Include output from other servlet.

Includes the response of another servlet in the current servlet’s response. See Application.includeURL for details. The main difference is that here you don’t have to pass in the transaction as the first argument.

lastModified(_trans)

Get time of last modification.

Return this object’s Last-Modified time (as a float), or None (meaning don’t know or not applicable).

log(message)

Log a message.

This can be invoked to print messages concerning the servlet. This is often used by self to relay important information back to developers.

methodNameForAction(name)

Return method name for an action name.

Invoked by _respond() to determine the method name for a given action name which has been derived as the value of an _action_ field. Since this is usually the label of an HTML submit button in a form, it is often needed to transform it in order to get a valid method name (for instance, blanks could be replaced by underscores and the like). This default implementation of the name transformation is the identity, it simply returns the name. Subclasses should override this method when action names don’t match their method names; they could “mangle” the action names or look the method names up in a dictionary.

name()

Return the name which is simple the name of the class.

Subclasses should not override this method. It is used for logging and debugging.

static notImplemented(trans)
open()
outputEncoding()

Get the default output encoding of the application.

postAction(actionName)

Things to do after action.

Invoked by self after invoking an action method. Subclasses may override to customize and may or may not invoke super as they see fit. The actionName is passed to this method, although it seems a generally bad idea to rely on this. However, it’s still provided just in case you need that hook.

By default, this does nothing.

preAction(actionName)

Things to do before action.

Invoked by self prior to invoking an action method. The actionName is passed to this method, although it seems a generally bad idea to rely on this. However, it’s still provided just in case you need that hook.

By default, this does nothing.

request()

The request (HTTPRequest) we’re handling.

respond(transaction)

Respond to a request.

Invokes the appropriate respondToSomething() method depending on the type of request (e.g., GET, POST, PUT, …).

respondToGet(transaction)

Respond to GET.

Invoked in response to a GET request method. All methods are passed to _respond.

respondToHead(trans)

Respond to a HEAD request.

A correct but inefficient implementation.

respondToPost(transaction)

Respond to POST.

Invoked in response to a POST request method. All methods are passed to _respond.

response()

The response (HTTPResponse) we’re handling.

runMethodForTransaction(transaction, method, *args, **kw)
static runTransaction(transaction)
sendRedirectAndEnd(url, status=None)

Send redirect and end.

Sends a redirect back to the client and ends the response. This is a very popular pattern.

sendRedirectPermanentAndEnd(url)

Send permanent redirect and end.

sendRedirectSeeOtherAndEnd(url)

Send redirect to a URL to be retrieved with GET and end.

This is the proper method for the Post/Redirect/Get pattern.

sendRedirectTemporaryAndEnd(url)

Send temporary redirect and end.

serverSidePath(path=None)

Return the filesystem path of the page on the server.

session()

The session object.

This provides a state for the current user (associated with a browser instance, really). If no session exists, then a session will be created.

sessionEncode(url=None)

Utility function to access Session.sessionEncode.

Takes a url and adds the session ID as a parameter. This is for cases where you don’t know if the client will accepts cookies.

setFactory(factory)
sleep(transaction)

Let servlet sleep again.

We unset some variables. Very boring.

transaction()

The Transaction we’re currently handling.

static urlDecode(s)

Turn special % characters into actual characters.

This method does the same as the urllib.unquote_plus() function.

static urlEncode(s)

Quotes special characters using the % substitutions.

This method does the same as the urllib.quote_plus() function.

write(*args)

Write to output.

Writes the arguments, which are turned to strings (with str) and concatenated before being written to the response. Unicode strings must be encoded before they can be written.

writeExceptionReport(handler)

Write extra information to the exception report.

The handler argument is the exception handler, and information is written there (using writeTitle, write, and writeln). This information is added to the exception report.

See ExceptionHandler for more information.

writeln(*args)

Write to output with newline.

Writes the arguments (like write), adding a newline after. Unicode strings must be encoded before they can be written.

exception HTTPContent.HTTPContentError

Bases: Exception

HTTP content error

__init__(*args, **kwargs)
args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.