Package smisk :: Module core :: Class Application
[frames] | no frames]

Class Application

object --+
         |
        Application
Known Subclasses:

An application.

Simple example:

>>> from smisk import Application
>>> class MyApp(Application):
>>>   def service(self):
>>>     self.response.write('<h1>Hello World!</h1>')
>>>
>>> MyApp().run()

Example of standalone/listening/slave process:

>>> import smisk
>>> class MyApp(smisk.Application):
>>>   def service(self):
>>>     self.response.write('<h1>Hello World!</h1>')
>>>
>>> from smisk.core import bind
>>> smisk.bind('hostname:1234')
>>> MyApp().run()

It is also possible to use your own types to represent Requests and Responses. You set request_class and/or response_class to a type, before application_will_start() has been called. For example:

>>> from smisk import Application, Request
>>> class MyRequest(Request):
>>>   def from_internet_explorer(self):
>>>     return self.env.get('HTTP_USER_AGENT','').find('MSIE') != -1
>>>
>>> class MyApp(Application):
>>>   def __init__(self):
>>>     super(MyApp, self).__init__()
>>>     self.request_class = MyRequest
>>>
>>>   def service(self):
>>>     if self.request.from_internet_explorer():
>>>       self.response.write('<h1>Good bye, cruel World!</h1>')
>>>     else:
>>>       self.response.write('<h1>Hello World!</h1>')
>>>
>>> MyApp().run()
Instance Methods
 
__init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
a new object with type S, a subtype of T
__new__(T, S, ...)
 
application_did_stop(...)
Called when the application stops accepting incoming requests.
 
application_will_start(...)
Called just before the application starts accepting incoming requests.
None
error(...)
Service a error message.
None
exit(...)
Exit application.
None
run(...)
Run application.
None
service(...)
Service a request.

Inherited from object: __delattr__, __getattribute__, __hash__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Static Methods
Application
current(...)
Current application instance, if any.
Properties
Request request
Type request_class
Must be set before calling run()
Response response
Type response_class
Must be set before calling run()
smisk.session.Store sessions
Type sessions_class
Must be set before first access to sessions
bool show_traceback
Defaults to True.

Inherited from object: __class__

Method Details

__init__(...)
(Constructor)

 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides: object.__init__

__new__(T, S, ...)

 
Returns: a new object with type S, a subtype of T
Overrides: object.__new__

application_did_stop(...)

 

Called when the application stops accepting incoming requests.

The default implementations does nothing. It's ment to be overridden. :rtype: None

application_will_start(...)

 

Called just before the application starts accepting incoming requests.

The default implementations does nothing. It's ment to be overridden. :rtype: None

error(...)

 

Service a error message.

You might override this to display a custom error response, but it is recommended you use this implementation, or at least filter certain higher level exceptions and let the lower ones through to this handler.

Normally, this is what you do:

>>> class MyApp(Application):
>>>   def error(self, typ, val, tb):
>>>     if issubclass(MyExceptionType, typ):
>>>       self.nice_error_response(typ, val)
>>>     else:
>>>       Application.error(self, typ, val, tb)
>>>

What is sent as response depends on if output has started or not: If output has started, if Response.has_begun is True, calling this method will insert a HTML formatted error message at the end of what has already been sent. If output has not yet begun, any headers set will be discarded and a complete HTTP response will be sent, including the same HTML message describet earlier.

If show_traceback evaluates to true, the error message will also include a somewhat detailed backtrace. You should disable show_traceback in production environments.

Parameters:
  • typ (type) - Exception type
  • val (Exception) - Exception value
  • tb (object) - Traceback
Returns: None

See Also: Response.has_begun