Package cherrypy
[hide private]
[frames] | no frames]

Package cherrypy

source code

CherryPy is a pythonic, object-oriented HTTP framework.


CherryPy consists of not one, but four separate API layers.

The APPLICATION LAYER is the simplest. CherryPy applications are written as
a tree of classes and methods, where each branch in the tree corresponds to
a branch in the URL path. Each method is a 'page handler', which receives
GET and POST params as keyword arguments, and returns or yields the (HTML)
body of the response. The special method name 'index' is used for paths
that end in a slash, and the special method name 'default' is used to
handle multiple paths via a single handler. This layer also includes:

 * the 'exposed' attribute (and cherrypy.expose)
 * cherrypy.quickstart()
 * _cp_config attributes
 * cherrypy.tools (including cherrypy.session)
 * cherrypy.url()

The ENVIRONMENT LAYER is used by developers at all levels. It provides
information about the current request and response, plus the application
and server environment, via a (default) set of top-level objects:

 * cherrypy.request
 * cherrypy.response
 * cherrypy.engine
 * cherrypy.server
 * cherrypy.tree
 * cherrypy.config
 * cherrypy.thread_data
 * cherrypy.log
 * cherrypy.HTTPError, NotFound, and HTTPRedirect
 * cherrypy.lib

The EXTENSION LAYER allows advanced users to construct and share their own
plugins. It consists of:

 * Hook API
 * Tool API
 * Toolbox API
 * Dispatch API
 * Config Namespace API

Finally, there is the CORE LAYER, which uses the core API's to construct
the default components which are available at higher layers. You can think
of the default components as the 'reference implementation' for CherryPy.
Megaframeworks (and advanced users) may replace the default components
with customized or extended components. The core API's are:

 * Application API
 * Engine API
 * Request API
 * Server API
 * WSGI API

These API's are described in the `CherryPy specification <https://bitbucket.org/cherrypy/cherrypy/wiki/CherryPySpec>`_.


Version: 3.3.0

Submodules [hide private]

Classes [hide private]
  _GlobalLogManager
A site-wide LogManager; routes to app.log or global log as appropriate.
  _HandleSignalsPlugin
Handle signals from other processes based on the configured platform handlers above.
  _Serving
An interface for registering request and response objects.
  _ThreadData
A container for thread-specific data.
  _ThreadLocalProxy
  _TimeoutMonitor
Functions [hide private]
 
_buslog(msg, level) source code
 
_cherrypy_pydoc_resolve(thing, forceload=0)
Given an object or a path to an object, get the object and its name.
source code
 
expose(func=None, alias=None)
Expose the function, optionally providing an alias or set of aliases.
source code
 
popargs(*args, **kwargs)
A decorator for _cp_dispatch (cherrypy.dispatch.Dispatcher.dispatch_method_name).
source code
 
quickstart(root=None, script_name='', config=None)
Mount the given root, start the builtin server (and engine), then block.
source code
 
url(path='', qs='', script_name=None, base=None, relative=None)
Create an absolute URL for the given path.
source code
Variables [hide private]
  __package__ = 'cherrypy'
  _global_conf_alias = {'request.show_mismatched_params': False,...
  checker = _cpchecker.Checker()
  config = {'request.show_mismatched_params': False, 'request.sh...
  engine = Bus()
  log = _GlobalLogManager()
  request = _ThreadLocalProxy('request')
  response = _ThreadLocalProxy('response')
  server = cherrypy.server
  serving = _Serving()
  thread_data = _ThreadData()
  tools = Toolbox("tools")
  tree = _cptree.Tree()
Function Details [hide private]

popargs(*args, **kwargs)

source code 
A decorator for _cp_dispatch
(cherrypy.dispatch.Dispatcher.dispatch_method_name).

Optional keyword argument: handler=(Object or Function)

Provides a _cp_dispatch function that pops off path segments into
cherrypy.request.params under the names specified.  The dispatch
is then forwarded on to the next vpath element.

Note that any existing (and exposed) member function of the class that
popargs is applied to will override that value of the argument.  For
instance, if you have a method named "list" on the class decorated with
popargs, then accessing "/list" will call that function instead of popping
it off as the requested parameter.  This restriction applies to all
_cp_dispatch functions.  The only way around this restriction is to create
a "blank class" whose only function is to provide _cp_dispatch.

If there are path elements after the arguments, or more arguments
are requested than are available in the vpath, then the 'handler'
keyword argument specifies the next object to handle the parameterized
request.  If handler is not specified or is None, then self is used.
If handler is a function rather than an instance, then that function
will be called with the args specified and the return value from that
function used as the next object INSTEAD of adding the parameters to
cherrypy.request.args.

This decorator may be used in one of two ways:

As a class decorator:
@cherrypy.popargs('year', 'month', 'day')
class Blog:
    def index(self, year=None, month=None, day=None):
        #Process the parameters here; any url like
        #/, /2009, /2009/12, or /2009/12/31
        #will fill in the appropriate parameters.

    def create(self):
        #This link will still be available at /create.  Defined functions
        #take precedence over arguments.

Or as a member of a class:
class Blog:
    _cp_dispatch = cherrypy.popargs('year', 'month', 'day')
    #...

The handler argument may be used to mix arguments with built in functions.
For instance, the following setup allows different activities at the
day, month, and year level:

class DayHandler:
    def index(self, year, month, day):
        #Do something with this day; probably list entries

    def delete(self, year, month, day):
        #Delete all entries for this day

@cherrypy.popargs('day', handler=DayHandler())
class MonthHandler:
    def index(self, year, month):
        #Do something with this month; probably list entries

    def delete(self, year, month):
        #Delete all entries for this month

@cherrypy.popargs('month', handler=MonthHandler())
class YearHandler:
    def index(self, year):
        #Do something with this year

    #...

@cherrypy.popargs('year', handler=YearHandler())
class Root:
    def index(self):
        #...

quickstart(root=None, script_name='', config=None)

source code 
Mount the given root, start the builtin server (and engine), then block.

root: an instance of a "controller class" (a collection of page handler
    methods) which represents the root of the application.
script_name: a string containing the "mount point" of the application.
    This should start with a slash, and be the path portion of the URL
    at which to mount the given root. For example, if root.index() will
    handle requests to "http://www.example.com:8080/dept/app1/", then
    the script_name argument would be "/dept/app1".

    It MUST NOT end in a slash. If the script_name refers to the root
    of the URI, it MUST be an empty string (not "/").
config: a file or dict containing application config. If this contains
    a [global] section, those entries will be used in the global
    (site-wide) config.

url(path='', qs='', script_name=None, base=None, relative=None)

source code 
Create an absolute URL for the given path.

If 'path' starts with a slash ('/'), this will return
    (base + script_name + path + qs).
If it does not start with a slash, this returns
    (base + script_name [+ request.path_info] + path + qs).

If script_name is None, cherrypy.request will be used
to find a script_name, if available.

If base is None, cherrypy.request.base will be used (if available).
Note that you can use cherrypy.tools.proxy to change this.

Finally, note that this function can be used to obtain an absolute URL
for the current request path (minus the querystring) by passing no args.
If you call url(qs=cherrypy.request.query_string), you should get the
original browser URL (assuming no internal redirections).

If relative is None or not provided, request.app.relative_urls will
be used (if available, else False). If False, the output will be an
absolute URL (including the scheme, host, vhost, and script_name).
If True, the output will instead be a URL that is relative to the
current request path, perhaps including '..' atoms. If relative is
the string 'server', the output will instead be a URL that is
relative to the server root; i.e., it will start with a slash.


Variables Details [hide private]

_global_conf_alias

Value:
{'request.show_mismatched_params': False, 'request.show_tracebacks': F\
alse, 'log.screen': False, 'tools.encode.on': True, 'checker.on': Fals\
e, 'server.max_request_body_size': 0, 'engine.autoreload.on': False, '\
engine.deadlock_poll_freq': 0, 'log.error.file': '', 'server.socket_po\
rt': 54583, 'environment': 'production', 'server.socket_host': '127.0.\
0.1', 'server.max_request_header_size': 0}

config

Value:
{'request.show_mismatched_params': False, 'request.show_tracebacks': F\
alse, 'log.screen': False, 'tools.encode.on': True, 'checker.on': Fals\
e, 'server.max_request_body_size': 0, 'engine.autoreload.on': False, '\
engine.deadlock_poll_freq': 0, 'log.error.file': '', 'server.socket_po\
rt': 54583, 'environment': 'production', 'server.socket_host': '127.0.\
0.1', 'server.max_request_header_size': 0}