| 1 |
<?xml version="1.0" encoding="UTF-8"?> |
|---|
| 2 |
<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.5b1//EN" |
|---|
| 3 |
"http://www.oasis-open.org/docbook/xml/4.5b1/docbookx.dtd"> |
|---|
| 4 |
<section xmlns:db="http://docbook.org/docbook-ng" xmlns:xi="http://www.w3.org/2001/XInclude" |
|---|
| 5 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xml:id="apireference"> |
|---|
| 6 |
<title>API reference</title> |
|---|
| 7 |
<section id="cherrypy"> |
|---|
| 8 |
<title>cherrypy.thread_data</title> |
|---|
| 9 |
<para>This attribute holds attributes that map to this thread only.</para> |
|---|
| 10 |
</section> |
|---|
| 11 |
<section id="cherrypyrequest"> |
|---|
| 12 |
<title>cherrypy.request</title> |
|---|
| 13 |
<para>The cherrypy.request object contains request-related objects. Pretty lame description, |
|---|
| 14 |
but that's all it does; it's a big data dump. At the beginning of each HTTP request, the |
|---|
| 15 |
existing request object is destroyed, and a new one is created, (one request object for each |
|---|
| 16 |
thread). Therefore, CherryPy (and you yourself) can stick data into cherrypy.request and not |
|---|
| 17 |
worry about it conflicting with other requests.</para> |
|---|
| 18 |
<section> |
|---|
| 19 |
<title>cherrypy.request.remoteAddr</title> |
|---|
| 20 |
<para>This attribute is a string containing the IP address of the client. It will be an |
|---|
| 21 |
empty string if it is not available.</para> |
|---|
| 22 |
</section> |
|---|
| 23 |
<section> |
|---|
| 24 |
<title>cherrypy.request.remotePort</title> |
|---|
| 25 |
<para>This attribute is an int containing the TCP port number of the client. It will be |
|---|
| 26 |
-1 if it is not available.</para> |
|---|
| 27 |
</section> |
|---|
| 28 |
<section> |
|---|
| 29 |
<title>cherrypy.request.remoteHost</title> |
|---|
| 30 |
<para>This attribute is a string containing the remote hostname of the client.</para> |
|---|
| 31 |
</section> |
|---|
| 32 |
<section> |
|---|
| 33 |
<title>cherrypy.request.headers</title> |
|---|
| 34 |
<para>This attribute is a dictionary containing the received HTTP headers, with |
|---|
| 35 |
automatically titled keys (e.g., "Content-Type"). As it's a dictionary, no duplicates are |
|---|
| 36 |
allowed.</para> |
|---|
| 37 |
</section> |
|---|
| 38 |
<section> |
|---|
| 39 |
<title>cherrypy.request.header_list</title> |
|---|
| 40 |
<para>This attribute is a list of (header, value) tuples containing the received HTTP |
|---|
| 41 |
headers. In general, you probably want to use headers instead; this is only here in case |
|---|
| 42 |
you need to inspect duplicates in the request headers.</para> |
|---|
| 43 |
</section> |
|---|
| 44 |
<section> |
|---|
| 45 |
<title>cherrypy.request.requestLine</title> |
|---|
| 46 |
<para>This attribute is a string containing the first line of the raw HTTP request; for |
|---|
| 47 |
example, "GET /path/page HTTP/1.1".</para> |
|---|
| 48 |
</section> |
|---|
| 49 |
<section> |
|---|
| 50 |
<title>cherrypy.request.simple_cookie</title> |
|---|
| 51 |
<para>This attribute is a SimpleCookie instance from the standard library's Cookie module |
|---|
| 52 |
which contains the incoming cookie values from the client.</para> |
|---|
| 53 |
</section> |
|---|
| 54 |
<section> |
|---|
| 55 |
<title>cherrypy.request.rfile</title> |
|---|
| 56 |
<para>This attribute is the input stream to the client, if applicable. See |
|---|
| 57 |
cherrypy.request.processRequestBody for more information.</para> |
|---|
| 58 |
</section> |
|---|
| 59 |
<section> |
|---|
| 60 |
<title>cherrypy.request.body</title> |
|---|
| 61 |
<para>This attribute is the request entity body, if applicable. See |
|---|
| 62 |
cherrypy.request.processRequestBody for more information.</para> |
|---|
| 63 |
</section> |
|---|
| 64 |
<section> |
|---|
| 65 |
<title>cherrypy.request.processRequestBody</title> |
|---|
| 66 |
<para>This attribute specifies whether or not the request's body (request.rfile, which is |
|---|
| 67 |
POST or PUT data) will be handled by CherryPy. If True (the default for POST and PUT |
|---|
| 68 |
requests), then request.rfile will be consumed by CherryPy (and unreadable after that). |
|---|
| 69 |
If the request Content-Type is "application/x-www-form-urlencoded", then the rfile will |
|---|
| 70 |
be parsed and placed into request.params; otherwise, it will be available in |
|---|
| 71 |
request.body. If cherrypy.request.processRequestBody is False, then the rfile is not |
|---|
| 72 |
consumed, but will be readable by the exposed method.</para> |
|---|
| 73 |
</section> |
|---|
| 74 |
<section> |
|---|
| 75 |
<title>cherrypy.request.method</title> |
|---|
| 76 |
<para>This attribute is a string containing the HTTP request method, such as GET or |
|---|
| 77 |
POST.</para> |
|---|
| 78 |
</section> |
|---|
| 79 |
<section> |
|---|
| 80 |
<title>cherrypy.request.protocol</title> |
|---|
| 81 |
<para>This attribute is a string containing the HTTP protocol of the request in the form |
|---|
| 82 |
of HTTP/x.x</para> |
|---|
| 83 |
</section> |
|---|
| 84 |
<section> |
|---|
| 85 |
<title>cherrypy.request.version</title> |
|---|
| 86 |
<para>This attribute is a Version object which represents the HTTP protocol. It's the |
|---|
| 87 |
same os request.protocol, but allows easy comparisons like <code>if |
|---|
| 88 |
cherrypy.request.version >= "1.1": do_http_1_1_thing</code>.</para> |
|---|
| 89 |
</section> |
|---|
| 90 |
<section> |
|---|
| 91 |
<title>cherrypy.request.wsgi_environ</title> |
|---|
| 92 |
<para>This attribute is a dictionary containing the WSGI environment for the request. In |
|---|
| 93 |
non-WSGI settings (i.e., custom HTTP servers), it is absent.</para> |
|---|
| 94 |
</section> |
|---|
| 95 |
<section> |
|---|
| 96 |
<title>cherrypy.request.query_string</title> |
|---|
| 97 |
<para>This attribute is a string containing the query string of the request (the part of |
|---|
| 98 |
the URL following '?').</para> |
|---|
| 99 |
</section> |
|---|
| 100 |
<section> |
|---|
| 101 |
<title>cherrypy.request.path</title> |
|---|
| 102 |
<para>This attribute is a string containing the path of the resource the client |
|---|
| 103 |
requested.</para> |
|---|
| 104 |
</section> |
|---|
| 105 |
<section> |
|---|
| 106 |
<title>cherrypy.request.params</title> |
|---|
| 107 |
<para>This attribute is a dictionary containing the query string and POST arguments of |
|---|
| 108 |
this request.</para> |
|---|
| 109 |
</section> |
|---|
| 110 |
<section> |
|---|
| 111 |
<title>cherrypy.request.base</title> |
|---|
| 112 |
<para>This attribute is a string containing the root URL of the server. By default, it is |
|---|
| 113 |
equal to request.scheme://request.headers['Host'].</para> |
|---|
| 114 |
</section> |
|---|
| 115 |
<section> |
|---|
| 116 |
<title>cherrypy.request.browser_url</title> |
|---|
| 117 |
<para>This attribute is a string containing the URL the client requested. By default, it |
|---|
| 118 |
is equal to <code>request.base + request.path</code>, plus the querystring, if |
|---|
| 119 |
provided.</para> |
|---|
| 120 |
</section> |
|---|
| 121 |
<section> |
|---|
| 122 |
<title>cherrypy.request.objectPath</title> |
|---|
| 123 |
<para>This attribute is a string containing the path of the exposed method that will be |
|---|
| 124 |
called to handle this request. This is usually the same as cherrypy.request.path, but can |
|---|
| 125 |
be changed in a filter to change which method is actually called.</para> |
|---|
| 126 |
</section> |
|---|
| 127 |
<section> |
|---|
| 128 |
<title>cherrypy.request.originalPath</title> |
|---|
| 129 |
<para>This attribute is a string containing the original value of cherrypy.request.path, |
|---|
| 130 |
in case it is modified by a filter during the request.</para> |
|---|
| 131 |
</section> |
|---|
| 132 |
<section> |
|---|
| 133 |
<title>cherrypy.request.originalParamMap</title> |
|---|
| 134 |
<para>This attribute is a string containing the original value of |
|---|
| 135 |
cherrypy.request.params, in case it is modified by a filter during the request.</para> |
|---|
| 136 |
</section> |
|---|
| 137 |
<section> |
|---|
| 138 |
<title>cherrypy.request.scheme</title> |
|---|
| 139 |
<para>This attribute is a string containing the URL scheme used in this request. It is |
|---|
| 140 |
either "http" or "https".</para> |
|---|
| 141 |
</section> |
|---|
| 142 |
</section> |
|---|
| 143 |
<section id="cherrypyresponse"> |
|---|
| 144 |
<title>cherrypy.response</title> |
|---|
| 145 |
<para>The cherrypy.response object contains response-related objects. Pretty lame |
|---|
| 146 |
description, but that's all it does; it's a big data dump. At the beginning of each HTTP |
|---|
| 147 |
request, the existing response object is destroyed, and a new one is created, (one response |
|---|
| 148 |
object for each thread). Therefore, CherryPy (and you yourself) can stick data into |
|---|
| 149 |
cherrypy.response and not worry about it conflicting with other requests.</para> |
|---|
| 150 |
<section> |
|---|
| 151 |
<title>cherrypy.response.headers</title> |
|---|
| 152 |
<para>This attribute is a dictionary with automatically titled keys (e.g., |
|---|
| 153 |
"Content-Length"). It holds all outgoing HTTP headers to the client.</para> |
|---|
| 154 |
</section> |
|---|
| 155 |
<section> |
|---|
| 156 |
<title>cherrypy.response.header_list</title> |
|---|
| 157 |
<para>This attribute is a list of (header, value) tuples. It's not available until the |
|---|
| 158 |
response has been finalized; it's really only there in the extremely rare cases when you |
|---|
| 159 |
need duplicate response header_list. In general, you should use request.headers |
|---|
| 160 |
instead.</para> |
|---|
| 161 |
</section> |
|---|
| 162 |
<section> |
|---|
| 163 |
<title>cherrypy.response.simple_cookie</title> |
|---|
| 164 |
<para>This attribute is a SimpleCookie instance from the standard library's Cookie |
|---|
| 165 |
module. It contains the outgoing cookie values.</para> |
|---|
| 166 |
</section> |
|---|
| 167 |
<section> |
|---|
| 168 |
<title>cherrypy.response.body</title> |
|---|
| 169 |
<para>This attribute is originally just the return value of the exposed method, but by |
|---|
| 170 |
the end of the request it must be an iterable (usually a list or generator of strings) |
|---|
| 171 |
which will be the content of the HTTP response.</para> |
|---|
| 172 |
</section> |
|---|
| 173 |
<section> |
|---|
| 174 |
<title>cherrypy.response.status</title> |
|---|
| 175 |
<para>This attribute is a string containing the HTTP response code in the form "### |
|---|
| 176 |
Reason-Phrase", i.e. "200 OK". You may also set it to an int, in which case the response |
|---|
| 177 |
finalization process will supply a Reason-Phrase for you.</para> |
|---|
| 178 |
</section> |
|---|
| 179 |
<section> |
|---|
| 180 |
<title>cherrypy.response.version</title> |
|---|
| 181 |
<para>This attribute is a Version object, representing the HTTP protocol version of the |
|---|
| 182 |
response. This is not necessarily the value that will be written in the response! |
|---|
| 183 |
Instead, it should be used to determine which features are <emphasis>available</emphasis> |
|---|
| 184 |
for the response. For example, an HTTP server may send an HTTP/1.1 response even though |
|---|
| 185 |
the client is known to only understand HTTP/1.0—the response.version will be set to |
|---|
| 186 |
Version("1.0") to inform you of this, so that you (and CherryPy) can restrict the |
|---|
| 187 |
response to HTTP/1.0 features only.</para> |
|---|
| 188 |
</section> |
|---|
| 189 |
</section> |
|---|
| 190 |
<section id="cherrypyserver"> |
|---|
| 191 |
<title>cherrypy.server</title> |
|---|
| 192 |
<section> |
|---|
| 193 |
<title>cherrypy.server.start(init_only = False, server_class = _missing)</title> |
|---|
| 194 |
<para>Start the CherryPy Server. Simple websites may call this without any arguments, to |
|---|
| 195 |
run the default server. If init_only is False (the default), this function will block |
|---|
| 196 |
until KeyboardInterrupt or SystemExit is raised, so that the process will persist. When |
|---|
| 197 |
using one of the built-in HTTP servers, you should leave this set to False. You should |
|---|
| 198 |
only set it to True if you're running CherryPy as an extension to another HTTP server |
|---|
| 199 |
(for example, when using Apache and mod_python with CherryPy), in which case the foreign |
|---|
| 200 |
HTTP server should do its own process-management.</para> |
|---|
| 201 |
<para>Use the server_class argument to specify that you wish to use an HTTP server other |
|---|
| 202 |
than the default, built-in WSGIServer. If missing, config.get("server.class") will be |
|---|
| 203 |
checked for an alternate value; otherwise, the default is used. Possible alternate values |
|---|
| 204 |
(you may pass the class names as a string if you wish):</para> |
|---|
| 205 |
<itemizedlist> |
|---|
| 206 |
<listitem> |
|---|
| 207 |
<para><code>None</code>: this will not load any HTTP server. Note that this is |
|---|
| 208 |
not the default; the default (if server_class is not given) is to load the |
|---|
| 209 |
WSGIServer.</para> |
|---|
| 210 |
</listitem> |
|---|
| 211 |
<listitem> |
|---|
| 212 |
<para>Any other class (or dotted-name string): load a custom HTTP server.</para> |
|---|
| 213 |
</listitem> |
|---|
| 214 |
</itemizedlist> |
|---|
| 215 |
<para>You <emphasis>must</emphasis> call this function from Python's main thread, and set |
|---|
| 216 |
init_only to False, if you want CherryPy to shut down when KeyboardInterrupt or |
|---|
| 217 |
SystemExit are raised (including Ctrl-C). The only time you might want to do otherwise is |
|---|
| 218 |
if you run CherryPy as a Windows service, or as an extension to, say, mod_python, and |
|---|
| 219 |
even then, you might want to anyway.</para> |
|---|
| 220 |
</section> |
|---|
| 221 |
<section> |
|---|
| 222 |
<title>cherrypy.server.blocking</title> |
|---|
| 223 |
<para>If the "init_only" argument to server.start is True, this will be False, and |
|---|
| 224 |
vice-versa.</para> |
|---|
| 225 |
</section> |
|---|
| 226 |
<section> |
|---|
| 227 |
<title>cherrypy.server.httpserverclass</title> |
|---|
| 228 |
<para>Whatever HTTP server class is set in server.start will be stuck in here.</para> |
|---|
| 229 |
</section> |
|---|
| 230 |
<section> |
|---|
| 231 |
<title>cherrypy.server.httpserver</title> |
|---|
| 232 |
<para>Whatever HTTP server class is set in server.start will be instantiated and stuck in |
|---|
| 233 |
here.</para> |
|---|
| 234 |
</section> |
|---|
| 235 |
<section> |
|---|
| 236 |
<title>cherrypy.server.state</title> |
|---|
| 237 |
<para>One of three values, indicating the state of the server: <itemizedlist> |
|---|
| 238 |
<listitem> |
|---|
| 239 |
<para>STOPPED = 0: The server hasn't been started, and will not accept |
|---|
| 240 |
requests.</para> |
|---|
| 241 |
</listitem> |
|---|
| 242 |
<listitem> |
|---|
| 243 |
<para>STARTING = None: The server is in the process of starting, or an error |
|---|
| 244 |
occured while trying to start the server.</para> |
|---|
| 245 |
</listitem> |
|---|
| 246 |
<listitem> |
|---|
| 247 |
<para>STARTED = 1: The server has started (including an HTTP server if |
|---|
| 248 |
requested), and is ready to receive requests.</para> |
|---|
| 249 |
</listitem> |
|---|
| 250 |
</itemizedlist></para> |
|---|
| 251 |
</section> |
|---|
| 252 |
<section> |
|---|
| 253 |
<title>cherrypy.server.ready</title> |
|---|
| 254 |
<para>True if the server is ready to receive requests, false otherwise. Read-only.</para> |
|---|
| 255 |
</section> |
|---|
| 256 |
<section> |
|---|
| 257 |
<title>cherrypy.server.wait()</title> |
|---|
| 258 |
<para>Since server.start usually blocks, other threads need to be started before calling |
|---|
| 259 |
server.start; however, they often must wait for server.start to complete it's setup of |
|---|
| 260 |
the HTTP server. Use this function from other threads to make them wait for the HTTP |
|---|
| 261 |
server to be ready to receive requests.</para> |
|---|
| 262 |
</section> |
|---|
| 263 |
<section> |
|---|
| 264 |
<title>cherrypy.server.start_with_callback(func, args=(), kwargs={}, server_class = |
|---|
| 265 |
_missing)</title> |
|---|
| 266 |
<para>Since server.start usually blocks, use this to easily run another function in a new |
|---|
| 267 |
thread. It starts the new thread and then runs server.start. The new thread automatically |
|---|
| 268 |
waits for the server to finish its startup procedure.</para> |
|---|
| 269 |
</section> |
|---|
| 270 |
<section> |
|---|
| 271 |
<title>cherrypy.server.stop()</title> |
|---|
| 272 |
<para>Stop the CherryPy Server. Well, "suspend" might be a better term—this doesn't |
|---|
| 273 |
terminate the process.</para> |
|---|
| 274 |
</section> |
|---|
| 275 |
<section> |
|---|
| 276 |
<title>cherrypy.server.interrupt</title> |
|---|
| 277 |
<para>Usually None, set this to KeyboardInterrupt() or SystemExit() to shut down the |
|---|
| 278 |
entire process. That is, the new exception will be raised in the main thread.</para> |
|---|
| 279 |
</section> |
|---|
| 280 |
<section> |
|---|
| 281 |
<title>cherrypy.server.restart()</title> |
|---|
| 282 |
<para>Restart the CherryPy Server.</para> |
|---|
| 283 |
</section> |
|---|
| 284 |
<section> |
|---|
| 285 |
<title>cherrypy.server.on_start_server_list</title> |
|---|
| 286 |
<para>A list of functions that will be called when the server starts.</para> |
|---|
| 287 |
</section> |
|---|
| 288 |
<section> |
|---|
| 289 |
<title>cherrypy.server.on_stop_server_list</title> |
|---|
| 290 |
<para>A list of functions that will be called when the server stops.</para> |
|---|
| 291 |
</section> |
|---|
| 292 |
<section> |
|---|
| 293 |
<title>cherrypy.server.on_start_thread_list</title> |
|---|
| 294 |
<para>A list of functions that will be called when each request thread is started. Note |
|---|
| 295 |
that such threads do not need to be started or controlled by CherryPy; for example, when |
|---|
| 296 |
using CherryPy with mod_python, Apache will start and stop the request threads. |
|---|
| 297 |
Nevertheless, CherryPy will run the on_start_thread_list functions upon the first request |
|---|
| 298 |
using each distinct thread.</para> |
|---|
| 299 |
</section> |
|---|
| 300 |
<section> |
|---|
| 301 |
<title>cherrypy.server.on_stop_thread_list</title> |
|---|
| 302 |
<para>A list of functions that will be called when each request thread is stopped.</para> |
|---|
| 303 |
</section> |
|---|
| 304 |
<section> |
|---|
| 305 |
<title>cherrypy.server.request()</title> |
|---|
| 306 |
<para>HTTP servers should call this function to create a new Request and Response object. |
|---|
| 307 |
The return value is the Request object; call its <code>run</code> method to have the |
|---|
| 308 |
CherryPy core process the request data and populate the response.</para> |
|---|
| 309 |
</section> |
|---|
| 310 |
</section> |
|---|
| 311 |
<section id="cherrypyconfig"> |
|---|
| 312 |
<title>cherrypy.config</title> |
|---|
| 313 |
<section> |
|---|
| 314 |
<title>cherrypy.config.get(key, defaultValue = None, returnSection = False)</title> |
|---|
| 315 |
<para>This function returns the configuration value for the given key. The function |
|---|
| 316 |
checks if the setting is defined for the current request path; it walks up the request |
|---|
| 317 |
path until the key is found, or it returns the default value. If returnSection is True, |
|---|
| 318 |
the function returns the configuration path where the key is defined instead.</para> |
|---|
| 319 |
</section> |
|---|
| 320 |
<section> |
|---|
| 321 |
<title>cherrypy.config.getAll(key)</title> |
|---|
| 322 |
<para>The getAll function returns a list containing a (path, value) tuple for all |
|---|
| 323 |
occurences of the key within the request path. This function allows applications to |
|---|
| 324 |
inherit configuration data defined for parent paths.</para> |
|---|
| 325 |
</section> |
|---|
| 326 |
<section> |
|---|
| 327 |
<title>cherrypy.config.update(updateMap=None, file=None)</title> |
|---|
| 328 |
<para>Function to update the configuration map. The "updateMap" argument is a dictionary |
|---|
| 329 |
of the form {'sectionPath' : { } }. The "file" argument is the path to the configuration |
|---|
| 330 |
file.</para> |
|---|
| 331 |
</section> |
|---|
| 332 |
<section> |
|---|
| 333 |
<title>cherrypy.config.environments</title> |
|---|
| 334 |
<para>Dict containing config defaults for each named server.environment.</para> |
|---|
| 335 |
</section> |
|---|
| 336 |
</section> |
|---|
| 337 |
<section> |
|---|
| 338 |
<title>cherrypy.tree</title> |
|---|
| 339 |
<para>The Tree class is used to keep track of where applications are mounted. To "mount" an |
|---|
| 340 |
application means to have its root respond to a URL other than "/". By using |
|---|
| 341 |
<code>cherrypy.tree</code>, you can easily mount applications and remember where you mounted |
|---|
| 342 |
them!</para> |
|---|
| 343 |
<section> |
|---|
| 344 |
<title>cherrypy.tree.mount(app_root, baseurl=None, conf=None)</title> |
|---|
| 345 |
<para>Function to mount a tree of objects at the given baseurl, using the given |
|---|
| 346 |
configuration dict or filename. If baseurl is None or missing, it is assumed to be "/" |
|---|
| 347 |
unless the config specifies <code>[global] mount_point = "/path/to/approot"</code>. If |
|---|
| 348 |
conf is not None, then each of its sections (which should be a relative URL, like |
|---|
| 349 |
"/skins/deepblue/main") will be prefixed with the baseurl, so that config lookups are |
|---|
| 350 |
also "mounted" at the base URL.</para> |
|---|
| 351 |
<para>Note that, by using tree.mount, your approot may not be found at cherrypy.root; |
|---|
| 352 |
there may be several "dummy" objects placed in-between cherrypy.root and your |
|---|
| 353 |
application's root instance.</para> |
|---|
| 354 |
</section> |
|---|
| 355 |
<section> |
|---|
| 356 |
<title>cherrypy.tree.mount_points</title> |
|---|
| 357 |
<para>A dict of the form {baseurl: approot-instance}.</para> |
|---|
| 358 |
</section> |
|---|
| 359 |
<section> |
|---|
| 360 |
<title>cherrypy.tree.mount_point(path=None)</title> |
|---|
| 361 |
<para>A method which finds the appropriate baseurl for a given path. If path is None or |
|---|
| 362 |
missing, cherrypy.request.object_path is used. If multiple applications "contain" the |
|---|
| 363 |
given path, the longer baseurl is returned. That is, if App1 is mounted at "/" and App2 |
|---|
| 364 |
is mounted at "/path/to/app", then |
|---|
| 365 |
<code>cherrypy.tree.mount_point("/path/to/app/main")</code> will return |
|---|
| 366 |
"/path/to/app".</para> |
|---|
| 367 |
<para>Once you have obtained the baseurl using mount_point, you can obtain a reference to |
|---|
| 368 |
the application root object by looking up |
|---|
| 369 |
<code>cherrypy.tree.mount_points[baseurl]</code>.</para> |
|---|
| 370 |
</section> |
|---|
| 371 |
<section> |
|---|
| 372 |
<title>cherrypy.tree.url(path, mount_point=None)</title> |
|---|
| 373 |
<para>Prefixes the given path with the given mount_point, which isn't very exciting |
|---|
| 374 |
unless mount_point is None or missing, in which case, cherrypy.request.object_path will |
|---|
| 375 |
be used to find the current mount_point and prefix <emphasis>that</emphasis> to the given |
|---|
| 376 |
path.</para> |
|---|
| 377 |
</section> |
|---|
| 378 |
</section> |
|---|
| 379 |
<section id="exceptions"> |
|---|
| 380 |
<title>cherrypy exceptions</title> |
|---|
| 381 |
<section> |
|---|
| 382 |
<title>cherrypy.HTTPError</title> |
|---|
| 383 |
<para>This exception can be used to automatically send a response using a http status |
|---|
| 384 |
code, with an appropriate error page.</para> |
|---|
| 385 |
<section> |
|---|
| 386 |
<title>cherrypy.NotFound</title> |
|---|
| 387 |
<para>This exception is raised when CherryPy is unable to map a requested path to an |
|---|
| 388 |
internal method. It's a subclass of HTTPError (404).</para> |
|---|
| 389 |
</section> |
|---|
| 390 |
</section> |
|---|
| 391 |
<section> |
|---|
| 392 |
<title>cherrypy.HTTPRedirect</title> |
|---|
| 393 |
<para>This exception will force a HTTP redirect to the url (or urls) you provide it. |
|---|
| 394 |
Optionally, you may specify the 3xx HTTP status code you want it to return. If you do |
|---|
| 395 |
not specify a status, then 303 will be used for HTTP/1.1 responses, and 302 will be |
|---|
| 396 |
used for HTTP/1.0 responses.</para> |
|---|
| 397 |
</section> |
|---|
| 398 |
<section> |
|---|
| 399 |
<title>cherrypy.InternalRedirect</title> |
|---|
| 400 |
<para>This exception will redirect processing to another path within the site (without |
|---|
| 401 |
informing the client). Provide the new path as an argument when raising the exception. |
|---|
| 402 |
You may also provide a second "params" argument which will replace the current request |
|---|
| 403 |
params (usually a dict, but you may also supply a GET-param-style string). This exception |
|---|
| 404 |
is only handled from within page handlers and before_main filter methods.</para> |
|---|
| 405 |
</section> |
|---|
| 406 |
</section> |
|---|
| 407 |
<section id="lib"> |
|---|
| 408 |
<title>The CherryPy library</title> |
|---|
| 409 |
<section> |
|---|
| 410 |
<title>cherrypy.lib.cptools</title> |
|---|
| 411 |
<section> |
|---|
| 412 |
<title>ExposeItems</title> |
|---|
| 413 |
<para>Utility class that exposes a getitem-aware object. It does not provide index() |
|---|
| 414 |
or default() methods, and it does not expose the individual item objects - just the |
|---|
| 415 |
list or dict that contains them. User-specific index() and default() methods can be |
|---|
| 416 |
implemented by inheriting from this class.</para> |
|---|
| 417 |
</section> |
|---|
| 418 |
<section> |
|---|
| 419 |
<title>PositionalParametersAware</title> |
|---|
| 420 |
<para>Utility class that restores positional parameters functionality that was found |
|---|
| 421 |
in 2.0.0-beta.</para> |
|---|
| 422 |
</section> |
|---|
| 423 |
<section> |
|---|
| 424 |
<title>getAccept(headername)</title> |
|---|
| 425 |
<para>Returns a list of AcceptValue objects from the specified Accept-* header (or |
|---|
| 426 |
None if the header is not present). The list is sorted so that the most-preferred |
|---|
| 427 |
values are first in the list.</para> |
|---|
| 428 |
<para>Each AcceptValue object has a <code>value</code> attribute, a string which is |
|---|
| 429 |
the value itself. For example, if <code>headername</code> is "Accept-Encoding", the |
|---|
| 430 |
<code>value</code> attribute might be "gzip". It also has a (read-only) |
|---|
| 431 |
<code>qvalue</code> attribute, a float between 0 and 1 which specifies the client's |
|---|
| 432 |
preference for the value; higher numbers are preferred. Finally, each AcceptValue |
|---|
| 433 |
also has a <code>params</code> attribute, a dict; for most headers, this dict will |
|---|
| 434 |
only possess the original "q" value as a string.</para> |
|---|
| 435 |
<para>If <code>headername</code> is "Accept" (the default), then the params attribute |
|---|
| 436 |
may contain extra parameters which further differentiate the value. In addition, |
|---|
| 437 |
<code>params["q"]</code> may itself be an AcceptValue object, with its own |
|---|
| 438 |
<code>params</code> dict. Don't ask us why; ask the authors of the HTTP spec.</para> |
|---|
| 439 |
</section> |
|---|
| 440 |
<section> |
|---|
| 441 |
<title>getRanges(content_length)</title> |
|---|
| 442 |
<para>Returns a list of (start, stop) indices from a Range request header. Returns |
|---|
| 443 |
None if no such header is provided in the request. Each (start, stop) tuple will be |
|---|
| 444 |
composed of two ints, which are suitable for use in a slicing operation. That is, the |
|---|
| 445 |
header "Range: bytes=3-6", if applied against a Python string, is requesting |
|---|
| 446 |
resource[3:7]. This function will return the list [(3, 7)].</para> |
|---|
| 447 |
</section> |
|---|
| 448 |
<section> |
|---|
| 449 |
<title>headers</title> |
|---|
| 450 |
<para>A subclass of Python's builtin <code>dict</code> class; CherryPy's default |
|---|
| 451 |
<code>request.headers</code> and <code>response.headers</code> objects are instances |
|---|
| 452 |
of this class. The keys are automatically titled (<code>str(key).title()</code>) in |
|---|
| 453 |
order to provide case-insensitive comparisons and avoid duplicates.</para> |
|---|
| 454 |
</section> |
|---|
| 455 |
<section> |
|---|
| 456 |
<title>parseRequestLine(requestLine)</title> |
|---|
| 457 |
<para>Returns (<code>method, path, querystring, protocol</code>) from an HTTP |
|---|
| 458 |
requestLine. The default Request processor calls this function.</para> |
|---|
| 459 |
</section> |
|---|
| 460 |
<section> |
|---|
| 461 |
<title>parseQueryString(query_string, keep_blank_values=True)</title> |
|---|
| 462 |
<para>Returns a dict of <code>{'key': 'value'}</code> pairs from an HTTP "key=value" |
|---|
| 463 |
query string. Also handles server-side image map query strings. The default Request |
|---|
| 464 |
processor calls this function.</para> |
|---|
| 465 |
</section> |
|---|
| 466 |
<section> |
|---|
| 467 |
<title>paramsFromCGIForm(form)</title> |
|---|
| 468 |
<para>Returns a dict of <code>{'key': ''value'}</code> pairs from a |
|---|
| 469 |
<code>cgi.FieldStorage</code> object. The default Request processor calls this |
|---|
| 470 |
function.</para> |
|---|
| 471 |
</section> |
|---|
| 472 |
<section> |
|---|
| 473 |
<title>serveFile(path, contentType=None, disposition=None, name=None)</title> |
|---|
| 474 |
<para>Set status, headers, and body in order to serve the file at the given path. The |
|---|
| 475 |
Content-Type header will be set to the contentType arg, if provided. If not provided, |
|---|
| 476 |
the Content-Type will be guessed by the extension of the file. If disposition is not |
|---|
| 477 |
None, the Content-Disposition header will be set to "<disposition>; |
|---|
| 478 |
filename=<name>". If name is None, it will be set to the basename of path. If |
|---|
| 479 |
disposition is None, no Content-Disposition header will be written.</para> |
|---|
| 480 |
</section> |
|---|
| 481 |
</section> |
|---|
| 482 |
<section> |
|---|
| 483 |
<title>cherrypy.lib.covercp</title> |
|---|
| 484 |
<para>This module both provides code-coverage tools, and may also be run as a script. To |
|---|
| 485 |
use this module, or the coverage tools in the test suite, you need to download |
|---|
| 486 |
'coverage.py', either Gareth Rees' <ulink url="???">original implementation</ulink> or |
|---|
| 487 |
Ned Batchelder's <ulink url="http://www.nedbatchelder.com/code/modules/coverage.html"> |
|---|
| 488 |
enhanced version</ulink>.</para> |
|---|
| 489 |
<para>Set cherrypy.codecoverage to True to turn on coverage tracing. Then, use the |
|---|
| 490 |
covercp.serve() function to browse the results in a web browser. If you run this module |
|---|
| 491 |
as a script (i.e., from the command line), it will call serve() for you.</para> |
|---|
| 492 |
</section> |
|---|
| 493 |
<section> |
|---|
| 494 |
<title>cherrypy.lib.profiler</title> |
|---|
| 495 |
<para>You can profile any of your page handlers (exposed methods) as follows:</para> |
|---|
| 496 |
<example> |
|---|
| 497 |
<title>Profiling example</title> |
|---|
| 498 |
<programlisting><code>from cherrypy.lib import profile |
|---|
| 499 |
|
|---|
| 500 |
class Root: |
|---|
| 501 |
p = profile.Profiler("/path/to/profile/dir") |
|---|
| 502 |
|
|---|
| 503 |
def index(self): |
|---|
| 504 |
self.p.run(self._index) |
|---|
| 505 |
index.exposed = True |
|---|
| 506 |
|
|---|
| 507 |
def _index(self): |
|---|
| 508 |
return "Hello, world!" |
|---|
| 509 |
|
|---|
| 510 |
cherrypy.root = Root()</code></programlisting> |
|---|
| 511 |
</example> |
|---|
| 512 |
<para>Set the config entry: "profiling.on = True" if you'd rather turn on profiling for |
|---|
| 513 |
all requests. Then, use the serve() function to browse the results in a web browser. If |
|---|
| 514 |
you run this module as a script (i.e., from the command line), it will call serve() for |
|---|
| 515 |
you.</para> |
|---|
| 516 |
<para>Developers: this module should be used whenever you make significant changes to |
|---|
| 517 |
CherryPy, to get a quick sanity-check on the performance of the request process. Basic |
|---|
| 518 |
requests should complete in about 5 milliseconds on a reasonably-fast machine running |
|---|
| 519 |
Python 2.4 (Python 2.3 will be much slower due to threadlocal being implemented in |
|---|
| 520 |
Python, not C). You can profile the test suite by supplying the --profile option to |
|---|
| 521 |
test.py.</para> |
|---|
| 522 |
</section> |
|---|
| 523 |
<section> |
|---|
| 524 |
<title>cherrypy.lib.autoreload</title> |
|---|
| 525 |
<para>This module provides a brute-force method of reloading application files on the |
|---|
| 526 |
fly. When the config entry "autoreload.on" is True (or when "server.environment" is |
|---|
| 527 |
"development"), CherryPy uses the autoreload module to restart the current process |
|---|
| 528 |
whenever one of the files in use is changed. The mechanism by which it does so is pretty |
|---|
| 529 |
complicated: <figure> |
|---|
| 530 |
<title>The autoreload process</title> |
|---|
| 531 |
<mediaobject> |
|---|
| 532 |
<imageobject> |
|---|
| 533 |
<imagedata fileref="autoreload.gif" format="GIF" /> |
|---|
| 534 |
</imageobject> |
|---|
| 535 |
</mediaobject> |
|---|
| 536 |
</figure></para> |
|---|
| 537 |
</section> |
|---|
| 538 |
</section> |
|---|
| 539 |
<section id="specialfunctions"> |
|---|
| 540 |
<title>Special functions and attributes</title> |
|---|
| 541 |
<section> |
|---|
| 542 |
<title>_cp_on_error</title> |
|---|
| 543 |
<para>_cp_on_error is a function for handling unanticipated exceptions, whether raised by |
|---|
| 544 |
CherryPy itself, or in user applications. The default simply responds as if |
|---|
| 545 |
HTTPError(500) had been raised.</para> |
|---|
| 546 |
</section> |
|---|
| 547 |
<section> |
|---|
| 548 |
<title>_cp_on_http_error</title> |
|---|
| 549 |
<para>_cp_on_http_error handles HTTPError responses, setting cherrypy.response.status, |
|---|
| 550 |
headers, and body.</para> |
|---|
| 551 |
</section> |
|---|
| 552 |
<section> |
|---|
| 553 |
<title>_cp_filters</title> |
|---|
| 554 |
<para>User defined filters are enabled using the class attribute _cp_filters. Any filter |
|---|
| 555 |
instances placed in _cp_filters will be applied to all methods of the class.</para> |
|---|
| 556 |
</section> |
|---|
| 557 |
<section> |
|---|
| 558 |
<title>_cp_log_access</title> |
|---|
| 559 |
<para>Function to log HTTP requests into the access.log file.</para> |
|---|
| 560 |
</section> |
|---|
| 561 |
<section> |
|---|
| 562 |
<title>_cp_log_message</title> |
|---|
| 563 |
<para>Function to log errors into the error.log file. The <code>cherrypy.log</code> |
|---|
| 564 |
function is syntactic sugar for this one.</para> |
|---|
| 565 |
</section> |
|---|
| 566 |
</section> |
|---|
| 567 |
<section id="filters"> |
|---|
| 568 |
<title>Filter API</title> |
|---|
| 569 |
<para>CherryPy provides a set of hooks which are called at specific places during the request |
|---|
| 570 |
process. A filter should inherit from the BaseFilter class and implement the hooks it |
|---|
| 571 |
requires to add extra code during the process. CherryPy will go through all the filters which |
|---|
| 572 |
are on (buil-in and user defined) for that requested path and call all hooks that are |
|---|
| 573 |
implemented by each filter.</para> |
|---|
| 574 |
<section> |
|---|
| 575 |
<title>on_start_resource</title> |
|---|
| 576 |
<para>This hook is being called righ at the beginning of the request process. The only |
|---|
| 577 |
work CherryPy has done when this hook is called is to parse the first line of the HTTP |
|---|
| 578 |
request. This is needed so that filters have access to the object path translated from |
|---|
| 579 |
the path specified in the HTTP request.</para> |
|---|
| 580 |
<para>This hook is always called.</para> |
|---|
| 581 |
</section> |
|---|
| 582 |
<section> |
|---|
| 583 |
<title>before_request_body</title> |
|---|
| 584 |
<para>This hook is being called right after CherryPy has parse the HTTP request headers |
|---|
| 585 |
but before it tries to parse the request body. If a filter which implements that hook |
|---|
| 586 |
sets cherrypy.request.processRequestBody to False, CherryPy will not parse the request |
|---|
| 587 |
body at all. This can be handy when you know your user agent returns the data in a form |
|---|
| 588 |
that the default CherryPy request body parsing function cannot understand.</para> |
|---|
| 589 |
<para>For example, assuming your user agent returns you a request body which is an XML |
|---|
| 590 |
string unquoted, you may want a filter to parse that XML string and generates an XML DOM |
|---|
| 591 |
instance. Then the filter could add that instance to the cherrypy.request.params which in |
|---|
| 592 |
turns would be passed to your page handler like if it had actually been sent like that |
|---|
| 593 |
through the HTTP request. Therefore your filter has turned the XML string into an XML DOM |
|---|
| 594 |
instance transparently and makes your life easier. In that case you do not want CherryPy |
|---|
| 595 |
to parse the request body. It could also be used to scan the request body before it is |
|---|
| 596 |
being processed any further and decide to reject it if needed.</para> |
|---|
| 597 |
<para>This hook is not called if an error occurs during the process before hand.</para> |
|---|
| 598 |
</section> |
|---|
| 599 |
<section> |
|---|
| 600 |
<title>before_main</title> |
|---|
| 601 |
<para>This hook is called right before your page handler (exposed callable) is being |
|---|
| 602 |
called by CherryPy. It can be handy if considering HTTP request headers or body you may |
|---|
| 603 |
want not to call the page handler at all, then you would have to set |
|---|
| 604 |
cherrypy.request.executeMain to False.</para> |
|---|
| 605 |
<para>This hook is not called if an error occurs during the process before hand.</para> |
|---|
| 606 |
</section> |
|---|
| 607 |
<section> |
|---|
| 608 |
<title>before_finalize</title> |
|---|
| 609 |
<para>This hook is called right after the page handler has been processed (depending on |
|---|
| 610 |
the before_main hook behavior) and before CherryPy formats the final respone object. It |
|---|
| 611 |
helps you for example to check for what could have been returned by your page handler and |
|---|
| 612 |
change some headers of needed.</para> |
|---|
| 613 |
<para>This hook is not called if an error occurs during the process before hand.</para> |
|---|
| 614 |
</section> |
|---|
| 615 |
<section> |
|---|
| 616 |
<title>on_end_resource</title> |
|---|
| 617 |
<para>This hook is called at the end of the process so that you can finely tweak your |
|---|
| 618 |
HTTP response if needed (eg adding headers to the cherrypy.response.header_list). Note |
|---|
| 619 |
that cherrypy.response.headers will not be processed any longer at that stage.</para> |
|---|
| 620 |
<para>This hook is always called.</para> |
|---|
| 621 |
</section> |
|---|
| 622 |
<section> |
|---|
| 623 |
<title>before_error_response</title> |
|---|
| 624 |
<para>This hook is called when an error has occured during the request processing. It |
|---|
| 625 |
allows you to called code before the _cp_on_error handler is being called as well as the |
|---|
| 626 |
response finalizing stage.</para> |
|---|
| 627 |
</section> |
|---|
| 628 |
<section> |
|---|
| 629 |
<title>after_error_response</title> |
|---|
| 630 |
<para>This hook is called when an error has occured during the request processing. It |
|---|
| 631 |
allows you to call code after the _cp_on_error handler is being called as well as the |
|---|
| 632 |
response finalizing stage.</para> |
|---|
| 633 |
</section> |
|---|
| 634 |
</section> |
|---|
| 635 |
</section> |
|---|