Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/branches/cherrypy-2.1/docs/book/xml/apireference.xml

Revision 753 (checked in by fumanchu, 3 years ago)

Docs: browserUrl also contains querystring if present.

Line 
1 <?xml version="1.0" encoding="UTF-8"?>
2 <section xmlns:db="http://docbook.org/docbook-ng" xmlns:xi="http://www.w3.org/2001/XInclude"
3          xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xml:id="apireference">
4     <title>API reference</title>
5     <section id="cherrypy">
6         <title>cherrypy.threadData</title>
7         <para>This attribute holds attributes that map to this thread only.</para>
8     </section>
9     <section id="cherrypyrequest">
10         <title>cherrypy.request</title>
11         <para>The cherrypy.request object contains request-related objects. Pretty lame description,
12         but that's all it does; it's a big data dump. At the beginning of each HTTP request, the
13         existing request object is destroyed, and a new one is created, (one request object for each
14         thread). Therefore, CherryPy (and you yourself) can stick data into cherrypy.request and not
15         worry about it conflicting with other requests.</para>
16         <section>
17             <title>cherrypy.request.remoteAddr</title>
18             <para>This attribute is a string containing the IP address of the client. It will be an
19             empty string if it is not available.</para>
20         </section>
21         <section>
22             <title>cherrypy.request.remotePort</title>
23             <para>This attribute is an int containing the TCP port number of the client. It will be
24             -1 if it is not available.</para>
25         </section>
26         <section>
27             <title>cherrypy.request.remoteHost</title>
28             <para>This attribute is a string containing the remote hostname of the client.</para>
29         </section>
30         <section>
31             <title>cherrypy.request.headerMap</title>
32             <para>This attribute is a dictionary containing the received HTTP headers, with
33             automatically titled keys (e.g., "Content-Type"). As it's a dictionary, no duplicates are
34             allowed.</para>
35         </section>
36         <section>
37             <title>cherrypy.request.headers</title>
38             <para>This attribute is a list of (header, value) tuples containing the received HTTP
39             headers. In general, you probably want to use headerMap instead; this is only here in
40             case you need to inspect duplicates in the request headers.</para>
41         </section>
42         <section>
43             <title>cherrypy.request.requestLine</title>
44             <para>This attribute is a string containing the first line of the raw HTTP request; for
45             example, "GET /path/page HTTP/1.1".</para>
46         </section>
47         <section>
48             <title>cherrypy.request.simpleCookie</title>
49             <para>This attribute is a SimpleCookie instance from the standard library's Cookie module
50             which contains the incoming cookie values from the client.</para>
51         </section>
52         <section>
53             <title>cherrypy.request.rfile</title>
54             <para>This attribute is the input stream to the client, if applicable. See
55             cherrypy.request.processRequestBody for more information.</para>
56         </section>
57         <section>
58             <title>cherrypy.request.body</title>
59             <para>This attribute is the request entity body, if applicable. See
60             cherrypy.request.processRequestBody for more information.</para>
61         </section>
62         <section>
63             <title>cherrypy.request.processRequestBody</title>
64             <para>This attribute specifies whether or not the request's body (request.rfile, which is
65             POST or PUT data) will be handled by CherryPy. If True (the default for POST and PUT
66             requests), then request.rfile will be consumed by CherryPy (and unreadable after that).
67             If the request Content-Type is "application/x-www-form-urlencoded", then the rfile will
68             be parsed and placed into request.paramMap; otherwise, it will be available in
69             request.body. If cherrypy.request.processRequestBody is False, then the rfile is not
70             consumed, but will be readable by the exposed method.</para>
71         </section>
72         <section>
73             <title>cherrypy.request.method</title>
74             <para>This attribute is a string containing the HTTP request method, such as GET or
75             POST.</para>
76         </section>
77         <section>
78             <title>cherrypy.request.protocol</title>
79             <para>This attribute is a string containing the HTTP protocol of the request in the form
80             of HTTP/x.x</para>
81         </section>
82         <section>
83             <title>cherrypy.request.version</title>
84             <para>This attribute is a Version object which represents the HTTP protocol. It's the
85             same os request.protocol, but allows easy comparisons like <code>if
86             cherrypy.request.version &gt;= "1.1": do_http_1_1_thing</code>.</para>
87         </section>
88         <section>
89             <title>cherrypy.request.queryString</title>
90             <para>This attribute is a string containing the query string of the request (the part of
91             the URL following '?').</para>
92         </section>
93         <section>
94             <title>cherrypy.request.path</title>
95             <para>This attribute is a string containing the path of the resource the client
96             requested.</para>
97         </section>
98         <section>
99             <title>cherrypy.request.paramMap</title>
100             <para>This attribute is a dictionary containing the query string and POST arguments of
101             this request.</para>
102         </section>
103         <section>
104             <title>cherrypy.request.base</title>
105             <para>This attribute is a string containing the root URL of the server. By default, it is
106             equal to request.scheme://request.headerMap['Host'].</para>
107         </section>
108         <section>
109             <title>cherrypy.request.browserUrl</title>
110             <para>This attribute is a string containing the URL the client requested. By default, it
111             is equal to <code>request.base + request.path</code>, plus the querystring, if
112             provided.</para>
113         </section>
114         <section>
115             <title>cherrypy.request.objectPath</title>
116             <para>This attribute is a string containing the path of the exposed method that will be
117             called to handle this request. This is usually the same as cherrypy.request.path, but can
118             be changed in a filter to change which method is actually called.</para>
119         </section>
120         <section>
121             <title>cherrypy.request.originalPath</title>
122             <para>This attribute is a string containing the original value of cherrypy.request.path,
123             in case it is modified by a filter during the request.</para>
124         </section>
125         <section>
126             <title>cherrypy.request.originalParamMap</title>
127             <para>This attribute is a string containing the original value of
128             cherrypy.request.paramMap, in case it is modified by a filter during the request.</para>
129         </section>
130         <section>
131             <title>cherrypy.request.scheme</title>
132             <para>This attribute is a string containing the URL scheme used in this request. It is
133             either "http" or "https".</para>
134         </section>
135     </section>
136     <!-- end cherrypy.request section -->
137     <section id="cherrypyresponse">
138         <title>cherrypy.response</title>
139         <para>The cherrypy.response object contains response-related objects. Pretty lame
140         description, but that's all it does; it's a big data dump. At the beginning of each HTTP
141         request, the existing response object is destroyed, and a new one is created, (one response
142         object for each thread). Therefore, CherryPy (and you yourself) can stick data into
143         cherrypy.response and not worry about it conflicting with other requests.</para>
144         <section>
145             <title>cherrypy.response.headerMap</title>
146             <para>This attribute is a dictionary with automatically titled keys (e.g.,
147             "Content-Length"). It holds all outgoing HTTP headers to the client.</para>
148         </section>
149         <section>
150             <title>cherrypy.response.headers</title>
151             <para>This attribute is a list of (header, value) tuples. It's not available until the
152             response has been finalized; it's really only there in the extremely rare cases when you
153             need duplicate response headers. In general, you should use request.headerMap
154             instead.</para>
155         </section>
156         <section>
157             <title>cherrypy.response.simpleCookie</title>
158             <para>This attribute is a SimpleCookie instance from the standard library's Cookie
159             module. It contains the outgoing cookie values.</para>
160         </section>
161         <section>
162             <title>cherrypy.response.body</title>
163             <para>This attribute is originally just the return value of the exposed method, but by
164             the end of the request it must be an iterable (usually a list or generator of strings)
165             which will be the content of the HTTP response.</para>
166         </section>
167         <section>
168             <title>cherrypy.response.status</title>
169             <para>This attribute is a string containing the HTTP response code in the form "###
170             Reason-Phrase", i.e. "200 OK". You may also set it to an int, in which case the response
171             finalization process will supply a Reason-Phrase for you.</para>
172         </section>
173         <section>
174             <title>cherrypy.response.version</title>
175             <para>This attribute is a Version object, representing the HTTP protocol version of the
176             response. This is not necessarily the value that will be written in the response!
177             Instead, it should be used to determine which features are <emphasis>available</emphasis>
178             for the response. For example, an HTTP server may send an HTTP/1.1 response even though
179             the client is known to only understand HTTP/1.0—the response.version will be set to
180             Version("1.0") to inform you of this, so that you (and CherryPy) can restrict the
181             response to HTTP/1.0 features only.</para>
182         </section>
183     </section>
184     <section id="cherrypyserver">
185         <title>cherrypy.server</title>
186         <section>
187             <title>cherrypy.server.start(initOnly=False, serverClass=_missing)</title>
188             <para>Start the CherryPy Server. Simple websites may call this without any arguments, to
189             run the default server. If initOnly is False (the default), this function will block
190             until KeyboardInterrupt or SystemExit is raised, so that the process will persist. When
191             using one of the built-in HTTP servers, you should leave this set to False. You should
192             only set it to True if you're running CherryPy as an extension to another HTTP server
193             (for example, when using Apache and mod_python with CherryPy), in which case the foreign
194             HTTP server should do its own process-management.</para>
195             <para>Use the serverClass argument to specify that you wish to use an HTTP server other
196             than the default, built-in WSGIServer. If missing, config.get("server.class") will be
197             checked for an alternate value; otherwise, the default is used. Possible alternate values
198             (you may pass the class names as a string if you wish):</para>
199             <itemizedlist>
200                 <listitem>
201                     <para><code>cherrypy._cphttpserver.CherryHTTPServer</code>: this will load the
202                     old, single-threaded built-in HTTP server. This server is deprecated and will
203                     probably be removed in CherryPy 2.2.</para>
204                 </listitem>
205                 <listitem>
206                     <para><code>cherrypy._cphttpserver.PooledThreadServer</code>: this will load the
207                     old, multi-threaded built-in HTTP server. This server is deprecated and will
208                     probably be removed in CherryPy 2.2.</para>
209                 </listitem>
210                 <listitem>
211                     <para><code>cherrypy._cphttpserver.embedded_server</code>: use this to
212                     automatically select between the CherryHTTPServer and the PooledThreadServer
213                     based on the value of config.get("server.threadPool") and
214                     config.get("server.socketFile").</para>
215                 </listitem>
216                 <listitem>
217                     <para><code>None</code>: this will not load any HTTP server. Note that this is
218                     not the default; the default (if serverClass is not given) is to load the
219                     WSGIServer.</para>
220                 </listitem>
221                 <listitem>
222                     <para>Any other class (or dotted-name string): load a custom HTTP server.</para>
223                 </listitem>
224             </itemizedlist>
225             <para>You <emphasis>must</emphasis> call this function from Python's main thread, and set
226             initOnly to False, if you want CherryPy to shut down when KeyboardInterrupt or SystemExit
227             are raised (including Ctrl-C). The only time you might want to do otherwise is if you run
228             CherryPy as a Windows service, or as an extension to, say, mod_python, and even then, you
229             might want to anyway.</para>
230         </section>
231         <section>
232             <title>cherrypy.server.blocking</title>
233             <para>If the "initOnly" argument to server.start is True, this will be False, and
234             vice-versa.</para>
235         </section>
236         <section>
237             <title>cherrypy.server.httpserverclass</title>
238             <para>Whatever HTTP server class is set in server.start will be stuck in here.</para>
239         </section>
240         <section>
241             <title>cherrypy.server.httpserver</title>
242             <para>Whatever HTTP server class is set in server.start will be instantiated and stuck in
243             here.</para>
244         </section>
245         <section>
246             <title>cherrypy.server.state</title>
247             <para>One of three values, indicating the state of the server:<itemizedlist>
248                     <listitem>
249                         <para>STOPPED = 0: The server hasn't been started, and will not accept
250                         requests.</para>
251                     </listitem>
252                     <listitem>
253                         <para>STARTING = None: The server is in the process of starting, or an error
254                         occured while trying to start the server.</para>
255                     </listitem>
256                     <listitem>
257                         <para>STARTED = 1: The server has started (including an HTTP server if
258                         requested), and is ready to receive requests.</para>
259                     </listitem>
260                 </itemizedlist></para>
261         </section>
262         <section>
263             <title>cherrypy.server.ready</title>
264             <para>True if the server is ready to receive requests, false otherwise. Read-only.</para>
265         </section>
266         <section>
267             <title>cherrypy.server.wait()</title>
268             <para>Since server.start usually blocks, other threads need to be started before calling
269             server.start; however, they often must wait for server.start to complete it's setup of
270             the HTTP server. Use this function from other threads to make them wait for the HTTP
271             server to be ready to receive requests.</para>
272         </section>
273         <section>
274             <title>cherrypy.server.start_with_callback(func, args=(), kwargs={},
275             serverClass=_missing)</title>
276             <para>Since server.start usually blocks, use this to easily run another function in a new
277             thread. It starts the new thread and then runs server.start. The new thread automatically
278             waits for the server to finish its startup procedure.</para>
279         </section>
280         <section>
281             <title>cherrypy.server.stop()</title>
282             <para>Stop the CherryPy Server. Well, "suspend" might be a better term—this doesn't
283             terminate the process.</para>
284         </section>
285         <section>
286             <title>cherrypy.server.interrupt</title>
287             <para>Usually None, set this to KeyboardInterrupt() or SystemExit() to shut down the
288             entire process. That is, the new exception will be raised in the main thread.</para>
289         </section>
290         <section>
291             <title>cherrypy.server.restart()</title>
292             <para>Restart the CherryPy Server.</para>
293         </section>
294         <section>
295             <title>cherrypy.server.onStartServerList</title>
296             <para>A list of functions that will be called when the server starts.</para>
297         </section>
298         <section>
299             <title>cherrypy.server.onStopServerList</title>
300             <para>A list of functions that will be called when the server stops.</para>
301         </section>
302         <section>
303             <title>cherrypy.server.onStartThreadList</title>
304             <para>A list of functions that will be called when each request thread is started. Note
305             that such threads do not need to be started or controlled by CherryPy; for example, when
306             using CherryPy with mod_python, Apache will start and stop the request threads.
307             Nevertheless, CherryPy will run the onStartThreadList functions upon the first request
308             using each distinct thread.</para>
309         </section>
310         <section>
311             <title>cherrypy.server.onStopThreadList</title>
312             <para>A list of functions that will be called when each request thread is stopped.</para>
313         </section>
314         <section>
315             <title>cherrypy.server.request()</title>
316             <para>HTTP servers should call this function to hand off the HTTP request to the CherryPy
317             core. There is no return value; instead, the core sets response content in the
318             cherrypy.response object.</para>
319         </section>
320     </section>
321     <!-- end cherrypyserver section -->
322     <section id="cherrypyconfig">
323         <title>cherrypy.config</title>
324         <section>
325             <title>cherrypy.config.get(key, defaultValue = None, returnSection = False)</title>
326             <para>This function returns the configuration value for the given key. The function
327             checks if the setting is defined for the current request path; it walks up the request
328             path until the key is found, or it returns the default value. If returnSection is True,
329             the function returns the configuration path where the key is defined instead.</para>
330         </section>
331         <section>
332             <title>cherrypy.config.getAll(key)</title>
333             <para>The getAll function returns a list containing a (path, value) tuple for all
334             occurences of the key within the request path. This function allows applications to
335             inherit configuration data defined for parent paths.</para>
336         </section>
337         <section>
338             <title>cherrypy.config.update(updateMap=None, file=None)</title>
339             <para>Function to update the configuration map. The "updateMap" argument is a dictionary
340             of the form {'sectionPath' : { } }. The "file" argument is the path to the configuration
341             file.</para>
342         </section>
343     </section>
344     <section id="exceptions">
345         <title>cherrypy exceptions</title>
346         <section>
347             <title>cherrypy.HTTPError</title>
348             <para>This exception can be used to automatically send a response using a http status
349             code, with an appropriate error page.</para>
350             <section>
351                 <title>cherrypy.NotFound</title>
352                 <para>This exception is raised when CherryPy is unable to map a requested path to an
353                 internal method. It's a subclass of HTTPError.</para>
354             </section>
355         </section>
356         <section>
357             <title>cherrypy.HTTPRedirect</title>
358             <para>This exception will force a HTTP redirect.</para>
359         </section>
360         <section>
361             <title>cherrypy.InternalRedirect</title>
362             <para>This exception will redirect processing to another path within the site (without
363             informing the client).</para>
364         </section>
365     </section>
366     <section id="lib">
367         <title>The CherryPy library</title>
368         <section>
369             <title>cherrypy.lib.cptools</title>
370             <section>
371                 <title>ExposeItems</title>
372                 <para>Utility class that exposes a getitem-aware object. It does not provide index()
373                 or default() methods, and it does not expose the individual item objects - just the
374                 list or dict that contains them. User-specific index() and default() methods can be
375                 implemented by inheriting from this class.</para>
376             </section>
377             <section>
378                 <title>PositionalParametersAware</title>
379                 <para>Utility class that restores positional parameters functionality that was found
380                 in 2.0.0-beta.</para>
381             </section>
382             <section>
383                 <title>getRanges(content_length)</title>
384                 <para>Returns a list of (start, stop) indices from a Range request header. Returns
385                 None if no such header is provided in the request. Each (start, stop) tuple will be
386                 composed of two ints, which are suitable for use in a slicing operation. That is, the
387                 header "Range: bytes=3-6", if applied against a Python string, is requesting
388                 resource[3:7]. This function will return the list [(3, 7)].</para>
389             </section>
390             <section>
391                 <title>serveFile(path, contentType=None, disposition=None, name=None)</title>
392                 <para>Set status, headers, and body in order to serve the file at the given path. The
393                 Content-Type header will be set to the contentType arg, if provided. If not provided,
394                 the Content-Type will be guessed by the extension of the file. If disposition is not
395                 None, the Content-Disposition header will be set to "&lt;disposition&gt;;
396                 filename=&lt;name&gt;". If name is None, it will be set to the basename of path. If
397                 disposition is None, no Content-Disposition header will be written.</para>
398             </section>
399         </section>
400         <section>
401             <title>cherrypy.lib.covercp</title>
402             <para>This module both provides code-coverage tools, and may also be run as a script. To
403             use this module, or the coverage tools in the test suite, you need to download
404             'coverage.py', either Gareth Rees' <ulink url="???">original implementation</ulink> or
405             Ned Batchelder's <ulink
406             url="http://www.nedbatchelder.com/code/modules/coverage.html">enhanced
407             version</ulink>.</para>
408             <para>Set cherrypy.codecoverage to True to turn on coverage tracing. Then, use the
409             covercp.serve() function to browse the results in a web browser. If you run this module
410             as a script (i.e., from the command line), it will call serve() for you.</para>
411         </section>
412         <section>
413             <title>cherrypy.lib.profiler</title>
414             <para>You can profile any of your page handlers (exposed methods) as follows:</para>
415             <example>
416                 <title>Profiling example</title>
417                 <programlisting><code>from cherrypy.lib import profile
418
419 class Root:
420     p = profile.Profiler("/path/to/profile/dir")
421    
422     def index(self):
423         self.p.run(self._index)
424     index.exposed = True
425    
426     def _index(self):
427         return "Hello, world!"
428
429 cherrypy.root = Root()</code></programlisting>
430             </example>
431             <para>Set the config entry: "profiling.on = True" if you'd rather turn on profiling for
432             all requests. Then, use the serve() function to browse the results in a web browser. If
433             you run this module as a script (i.e., from the command line), it will call serve() for
434             you.</para>
435             <para>Developers: this module should be used whenever you make significant changes to
436             CherryPy, to get a quick sanity-check on the performance of the request process. Basic
437             requests should complete in about 5 milliseconds on a reasonably-fast machine running
438             Python 2.4 (Python 2.3 will be much slower due to threadlocal being implemented in
439             Python, not C). You can profile the test suite by supplying the --profile option to
440             test.py.</para>
441         </section>
442         <section>
443             <title>cherrypy.lib.autoreload</title>
444             <para>This module provides a brute-force method of reloading application files on the
445             fly. When the config entry "autoreload.on" is True (or when "server.environment" is
446             "development"), CherryPy uses the autoreload module to restart the current process
447             whenever one of the files in use is changed. The mechanism by which it does so is pretty
448             complicated:<figure>
449                     <title>The autoreload process</title>
450                     <mediaobject>
451                         <imageobject>
452                             <imagedata fileref="autoreload.gif" format="GIF" />
453                         </imageobject>
454                     </mediaobject>
455                 </figure></para>
456         </section>
457     </section>
458     <section id="specialfunctions">
459         <title>Special functions and attributes</title>
460         <section>
461             <title>_cpOnError</title>
462             <para>_cpOnError is a function for handling unanticipated exceptions, whether raised by
463             CherryPy itself, or in user applications. The default simply raises
464             HTTPError(500).</para>
465         </section>
466         <section>
467             <title>_cpFilterList</title>
468             <para>User defined filters are enabled using the class attribute _cpFilterList. Any
469             filter instances placed in _cpFilterList will be applied to all methods of the
470             class.</para>
471         </section>
472     </section>
473     <section id="filters">
474         <title>Filter API</title>
475         <section>
476             <title>onStartResource</title>
477         </section>
478         <section>
479             <title>beforeRequestBody</title>
480         </section>
481         <section>
482             <title>beforeMain</title>
483         </section>
484         <section>
485             <title>beforeFinalize</title>
486         </section>
487         <section>
488             <title>onEndResource</title>
489         </section>
490         <section>
491             <title>beforeErrorResponse</title>
492         </section>
493         <section>
494             <title>afterErrorResponse</title>
495         </section>
496     </section>
497 </section>
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets