/usr/share/doc/python-cherrypy3-doc/html/config.html is in python-cherrypy3-doc 8.9.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Configure — CherryPy 8.9.1 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '8.9.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Extend" href="extend.html" />
<link rel="prev" title="Advanced" href="advanced.html" />
</head>
<body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="extend.html" title="Extend"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="advanced.html" title="Advanced"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">CherryPy 8.9.1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="configure">
<span id="configindepth"></span><h1><a class="toc-backref" href="#id3">Configure</a><a class="headerlink" href="#configure" title="Permalink to this headline">¶</a></h1>
<p>Configuration in CherryPy is implemented via dictionaries. Keys are strings
which name the mapped value; values may be of any type.</p>
<p>In CherryPy 3, you use configuration (files or dicts) to set attributes
directly on the engine, server, request, response, and log objects. So the
best way to know the full range of what’s available in the config file is to
simply import those objects and see what <code class="docutils literal"><span class="pre">help(obj)</span></code> tells you.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you are new to CherryPy, please refer first to the simpler
<a class="reference internal" href="basics.html#config"><span class="std std-ref">basic config</span></a> section first.</p>
</div>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#configure" id="id3">Configure</a><ul>
<li><a class="reference internal" href="#architecture" id="id4">Architecture</a><ul>
<li><a class="reference internal" href="#global-config" id="id5">Global config</a></li>
<li><a class="reference internal" href="#application-config" id="id6">Application config</a></li>
<li><a class="reference internal" href="#request-config" id="id7">Request config</a></li>
</ul>
</li>
<li><a class="reference internal" href="#declaration" id="id8">Declaration</a><ul>
<li><a class="reference internal" href="#configuration-files" id="id9">Configuration files</a></li>
<li><a class="reference internal" href="#cp-config-attaching-config-to-handlers" id="id10">_cp_config: attaching config to handlers</a></li>
</ul>
</li>
<li><a class="reference internal" href="#namespaces" id="id11">Namespaces</a><ul>
<li><a class="reference internal" href="#builtin-namespaces" id="id12">Builtin namespaces</a></li>
<li><a class="reference internal" href="#custom-config-namespaces" id="id13">Custom config namespaces</a></li>
<li><a class="reference internal" href="#environments" id="id14">Environments</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="architecture">
<h2><a class="toc-backref" href="#id4">Architecture</a><a class="headerlink" href="#architecture" title="Permalink to this headline">¶</a></h2>
<p>The first thing you need to know about CherryPy 3’s configuration is that it
separates <em>global</em> config from <em>application</em> config. If you’re deploying
multiple <em>applications</em> at the same <em>site</em> (and more and more people are,
as Python web apps are tending to decentralize), you need to be careful to
separate the configurations, as well. There’s only ever one “global config”,
but there is a separate “app config” for each app you deploy.</p>
<p>CherryPy <em>Requests</em> are part of an <em>Application</em>, which runs in a <em>global</em>
context, and configuration data may apply to any of those three scopes.
Let’s look at each of those scopes in turn.</p>
<div class="section" id="global-config">
<h3><a class="toc-backref" href="#id5">Global config</a><a class="headerlink" href="#global-config" title="Permalink to this headline">¶</a></h3>
<p>Global config entries apply everywhere, and are stored in
<code class="xref py py-class docutils literal"><span class="pre">cherrypy.config</span></code>. This flat dict only holds
global config data; that is, “site-wide” config entries which affect all
mounted applications.</p>
<p>Global config is stored in the
<code class="xref py py-class docutils literal"><span class="pre">cherrypy.config</span></code> dict,
and you therefore update it by calling <code class="docutils literal"><span class="pre">cherrypy.config.update(conf)</span></code>.
The <code class="docutils literal"><span class="pre">conf</span></code> argument can be either a filename, an open file, or a dict of
config entries. Here’s an example of passing a dict argument:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">cherrypy</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s1">'server.socket_host'</span><span class="p">:</span> <span class="s1">'64.72.221.48'</span><span class="p">,</span>
<span class="s1">'server.socket_port'</span><span class="p">:</span> <span class="mi">80</span><span class="p">,</span>
<span class="p">})</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">server.socket_host</span></code> option in this example determines on which network
interface CherryPy will listen. The <code class="docutils literal"><span class="pre">server.socket_port</span></code> option declares
the TCP port on which to listen.</p>
</div>
<div class="section" id="application-config">
<h3><a class="toc-backref" href="#id6">Application config</a><a class="headerlink" href="#application-config" title="Permalink to this headline">¶</a></h3>
<p>Application entries apply to a single mounted application, and are stored on
each Application object itself as
<code class="xref py py-attr docutils literal"><span class="pre">app.config</span></code>. This is a two-level
dict where each top-level key is a path, or “relative URL” (for example,
<code class="docutils literal"><span class="pre">"/"</span></code> or <code class="docutils literal"><span class="pre">"/my/page"</span></code>), and each value is a dict of config entries.
The URL’s are relative to the script name (mount point) of the Application.
Usually, all this data is provided in the call to
<code class="docutils literal"><span class="pre">tree.mount(root(),</span> <span class="pre">script_name='/path/to',</span> <span class="pre">config=conf)</span></code>,
although you may also use <code class="docutils literal"><span class="pre">app.merge(conf)</span></code>.
The <code class="docutils literal"><span class="pre">conf</span></code> argument can be either a filename, an open file, or a dict of
config entries.</p>
<p>Configuration file example:</p>
<div class="highlight-ini"><div class="highlight"><pre><span></span><span class="k">[/]</span>
<span class="na">tools.trailing_slash.on</span> <span class="o">=</span> <span class="s">False</span>
<span class="na">request.dispatch: cherrypy.dispatch.MethodDispatcher()</span>
</pre></div>
</div>
<p>or, in python code:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">config</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'/'</span><span class="p">:</span>
<span class="p">{</span>
<span class="s1">'request.dispatch'</span><span class="p">:</span> <span class="n">cherrypy</span><span class="o">.</span><span class="n">dispatch</span><span class="o">.</span><span class="n">MethodDispatcher</span><span class="p">(),</span>
<span class="s1">'tools.trailing_slash.on'</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">tree</span><span class="o">.</span><span class="n">mount</span><span class="p">(</span><span class="n">Root</span><span class="p">(),</span> <span class="n">config</span><span class="o">=</span><span class="n">config</span><span class="p">)</span>
</pre></div>
</div>
<p>CherryPy only uses sections that start with <code class="docutils literal"><span class="pre">"/"</span></code> (except
<code class="docutils literal"><span class="pre">[global]</span></code>, see below). That means you can place your own configuration
entries in a CherryPy config file by giving them a section name which does not
start with <code class="docutils literal"><span class="pre">"/"</span></code>. For example, you might include database entries like this:</p>
<div class="highlight-ini"><div class="highlight"><pre><span></span><span class="k">[global]</span>
<span class="na">server.socket_host: "0.0.0.0"</span>
<span class="k">[Databases]</span>
<span class="na">driver: "postgres"</span>
<span class="na">host: "localhost"</span>
<span class="na">port: 5432</span>
<span class="k">[/path]</span>
<span class="na">response.timeout: 6000</span>
</pre></div>
</div>
<p>Then, in your application code you can read these values during request time
via <code class="docutils literal"><span class="pre">cherrypy.request.app.config['Databases']</span></code>. For code that is outside the
request process, you’ll have to pass a reference to your Application around.</p>
</div>
<div class="section" id="request-config">
<h3><a class="toc-backref" href="#id7">Request config</a><a class="headerlink" href="#request-config" title="Permalink to this headline">¶</a></h3>
<p>Each Request object possesses a single
<code class="xref py py-attr docutils literal"><span class="pre">request.config</span></code> dict. Early in the
request process, this dict is populated by merging Global config, Application
config, and any config acquired while looking up the page handler (see next).
This dict contains only those config entries which apply to the given request.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">when you do an <code class="xref py py-class docutils literal"><span class="pre">InternalRedirect</span></code>,
this config attribute is recalculated for the new path.</p>
</div>
</div>
</div>
<div class="section" id="declaration">
<h2><a class="toc-backref" href="#id8">Declaration</a><a class="headerlink" href="#declaration" title="Permalink to this headline">¶</a></h2>
<p>Configuration data may be supplied as a Python dictionary, as a filename,
or as an open file object.</p>
<div class="section" id="configuration-files">
<h3><a class="toc-backref" href="#id9">Configuration files</a><a class="headerlink" href="#configuration-files" title="Permalink to this headline">¶</a></h3>
<p>When you supply a filename or file, CherryPy uses Python’s builtin ConfigParser;
you declare Application config by writing each path as a section header,
and each entry as a <code class="docutils literal"><span class="pre">"key:</span> <span class="pre">value"</span></code> (or <code class="docutils literal"><span class="pre">"key</span> <span class="pre">=</span> <span class="pre">value"</span></code>) pair:</p>
<div class="highlight-ini"><div class="highlight"><pre><span></span><span class="k">[/path/to/my/page]</span>
<span class="na">response.stream: True</span>
<span class="na">tools.trailing_slash.extra</span> <span class="o">=</span> <span class="s">False</span>
</pre></div>
</div>
<div class="section" id="combined-configuration-files">
<h4>Combined Configuration Files<a class="headerlink" href="#combined-configuration-files" title="Permalink to this headline">¶</a></h4>
<p>If you are only deploying a single application, you can make a single config
file that contains both global and app entries. Just stick the global entries
into a config section named <code class="docutils literal"><span class="pre">[global]</span></code>, and pass the same file to both
<code class="xref py py-func docutils literal"><span class="pre">config.update</span></code> and
<code class="xref py py-func docutils literal"><span class="pre">tree.mount</span> <span class="pre"><cherrypy._cptree.Tree.mount()</span></code>. If you’re calling
<code class="docutils literal"><span class="pre">cherrypy.quickstart(app</span> <span class="pre">root,</span> <span class="pre">script</span> <span class="pre">name,</span> <span class="pre">config)</span></code>, it will pass the
config to both places for you. But as soon as you decide to add another
application to the same site, you need to separate the two config files/dicts.</p>
</div>
<div class="section" id="separate-configuration-files">
<h4>Separate Configuration Files<a class="headerlink" href="#separate-configuration-files" title="Permalink to this headline">¶</a></h4>
<p>If you’re deploying more than one application in the same process, you need
(1) file for global config, plus (1) file for <em>each</em> Application.
The global config is applied by calling
<code class="xref py py-func docutils literal"><span class="pre">cherrypy.config.update</span></code>,
and application config is usually passed in a call to
<code class="xref py py-func docutils literal"><span class="pre">cherrypy.tree.mount</span></code>.</p>
<p>In general, you should set global config first, and then mount each
application with its own config. Among other benefits, this allows you to set
up global logging so that, if something goes wrong while trying to mount
an application, you’ll see the tracebacks. In other words, use this order:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># global config</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s1">'environment'</span><span class="p">:</span> <span class="s1">'production'</span><span class="p">,</span>
<span class="s1">'log.error_file'</span><span class="p">:</span> <span class="s1">'site.log'</span><span class="p">,</span>
<span class="c1"># ...</span>
<span class="p">})</span>
<span class="c1"># Mount each app and pass it its own config</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">tree</span><span class="o">.</span><span class="n">mount</span><span class="p">(</span><span class="n">root1</span><span class="p">,</span> <span class="s2">""</span><span class="p">,</span> <span class="n">appconf1</span><span class="p">)</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">tree</span><span class="o">.</span><span class="n">mount</span><span class="p">(</span><span class="n">root2</span><span class="p">,</span> <span class="s2">"/forum"</span><span class="p">,</span> <span class="n">appconf2</span><span class="p">)</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">tree</span><span class="o">.</span><span class="n">mount</span><span class="p">(</span><span class="n">root3</span><span class="p">,</span> <span class="s2">"/blog"</span><span class="p">,</span> <span class="n">appconf3</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">cherrypy</span><span class="o">.</span><span class="n">engine</span><span class="p">,</span> <span class="s1">'block'</span><span class="p">):</span>
<span class="c1"># 3.1 syntax</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">engine</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">engine</span><span class="o">.</span><span class="n">block</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="c1"># 3.0 syntax</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">server</span><span class="o">.</span><span class="n">quickstart</span><span class="p">()</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">engine</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="values-in-config-files-use-python-syntax">
<h4>Values in config files use Python syntax<a class="headerlink" href="#values-in-config-files-use-python-syntax" title="Permalink to this headline">¶</a></h4>
<p>Config entries are always a key/value pair, like <code class="docutils literal"><span class="pre">server.socket_port</span> <span class="pre">=</span> <span class="pre">8080</span></code>.
The key is always a name, and the value is always a Python object. That is,
if the value you are setting is an <code class="docutils literal"><span class="pre">int</span></code> (or other number), it needs to look
like a Python <code class="docutils literal"><span class="pre">int</span></code>; for example, <code class="docutils literal"><span class="pre">8080</span></code>. If the value is a string, it
needs to be quoted, just like a Python string. Arbitrary objects can also be
created, just like in Python code (assuming they can be found/imported).
Here’s an extended example, showing you some of the different types:</p>
<div class="highlight-ini"><div class="highlight"><pre><span></span><span class="k">[global]</span>
<span class="na">log.error_file: "/home/fumanchu/myapp.log"</span>
<span class="na">environment</span> <span class="o">=</span> <span class="s">'production'</span>
<span class="na">server.max_request_body_size: 1200</span>
<span class="k">[/myapp]</span>
<span class="na">tools.trailing_slash.on</span> <span class="o">=</span> <span class="s">False</span>
<span class="na">request.dispatch: cherrypy.dispatch.MethodDispatcher()</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="cp-config-attaching-config-to-handlers">
<span id="cp-config"></span><h3><a class="toc-backref" href="#id10">_cp_config: attaching config to handlers</a><a class="headerlink" href="#cp-config-attaching-config-to-handlers" title="Permalink to this headline">¶</a></h3>
<p>Config files have a severe limitation: values are always keyed by URL.
For example:</p>
<div class="highlight-ini"><div class="highlight"><pre><span></span><span class="k">[/path/to/page]</span>
<span class="na">methods_with_bodies</span> <span class="o">=</span> <span class="s">("POST", "PUT", "PROPPATCH")</span>
</pre></div>
</div>
<p>It’s obvious that the extra method is the norm for that path; in fact,
the code could be considered broken without it. In CherryPy, you can attach
that bit of config directly on the page handler:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="nd">@cherrypy.expose</span>
<span class="k">def</span> <span class="nf">page</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">"Hello, world!"</span>
<span class="n">page</span><span class="o">.</span><span class="n">_cp_config</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"request.methods_with_bodies"</span><span class="p">:</span> <span class="p">(</span><span class="s2">"POST"</span><span class="p">,</span> <span class="s2">"PUT"</span><span class="p">,</span> <span class="s2">"PROPPATCH"</span><span class="p">)}</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">_cp_config</span></code> is a reserved attribute which the dispatcher looks for at
each node in the object tree. The <code class="docutils literal"><span class="pre">_cp_config</span></code> attribute must be a CherryPy
config dictionary. If the dispatcher finds a <code class="docutils literal"><span class="pre">_cp_config</span></code> attribute,
it merges that dictionary into the rest of the config. The entire merged
config dictionary is placed in
<code class="xref py py-attr docutils literal"><span class="pre">cherrypy.request.config</span></code>.</p>
<p>This can be done at any point in the tree of objects; for example, we could have
attached that config to a class which contains the page method:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">SetOPages</span><span class="p">:</span>
<span class="n">_cp_config</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"request.methods_with_bodies"</span><span class="p">:</span> <span class="p">(</span><span class="s2">"POST"</span><span class="p">,</span> <span class="s2">"PUT"</span><span class="p">,</span> <span class="s2">"PROPPATCH"</span><span class="p">)}</span>
<span class="nd">@cherrypy.expose</span>
<span class="k">def</span> <span class="nf">page</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">"Hullo, Werld!"</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This behavior is only guaranteed for the default dispatcher. Other
dispatchers may have different restrictions on where you can attach
<code class="docutils literal"><span class="pre">_cp_config</span></code> attributes.</p>
</div>
<p>This technique allows you to:</p>
<blockquote>
<div><ul class="simple">
<li>Put config near where it’s used for improved readability and maintainability.</li>
<li>Attach config to objects instead of URL’s. This allows multiple URL’s to
point to the same object, yet you only need to define the config once.</li>
<li>Provide defaults which are still overridable in a config file.</li>
</ul>
</div></blockquote>
</div>
</div>
<div class="section" id="namespaces">
<span id="id1"></span><h2><a class="toc-backref" href="#id11">Namespaces</a><a class="headerlink" href="#namespaces" title="Permalink to this headline">¶</a></h2>
<p>Because config entries usually just set attributes on objects, they’re almost
all of the form: <code class="docutils literal"><span class="pre">object.attribute</span></code>. A few are of the form:
<code class="docutils literal"><span class="pre">object.subobject.attribute</span></code>. They look like normal Python attribute chains,
because they work like them. We call the first name in the chain the
<em>“config namespace”</em>. When you provide a config entry, it is bound as early
as possible to the actual object referenced by the namespace; for example,
the entry <code class="docutils literal"><span class="pre">response.stream</span></code> actually sets the <code class="docutils literal"><span class="pre">stream</span></code> attribute of
<code class="xref py py-class docutils literal"><span class="pre">cherrypy.response</span></code>! In this way,
you can easily determine the default value by firing up a python interpreter
and typing:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">cherrypy</span>
<span class="gp">>>> </span><span class="n">cherrypy</span><span class="o">.</span><span class="n">response</span><span class="o">.</span><span class="n">stream</span>
<span class="go">False</span>
</pre></div>
</div>
<p>Each config namespace has its own handler; for example, the “request” namespace
has a handler which takes your config entry and sets that value on the
appropriate “request” attribute. There are a few namespaces, however, which
don’t work like normal attributes behind the scenes; however, they still use
dotted keys and are considered to “have a namespace”.</p>
<div class="section" id="builtin-namespaces">
<h3><a class="toc-backref" href="#id12">Builtin namespaces</a><a class="headerlink" href="#builtin-namespaces" title="Permalink to this headline">¶</a></h3>
<p>Entries from each namespace may be allowed in the global, application root
(<code class="docutils literal"><span class="pre">"/"</span></code>) or per-path config, or a combination:</p>
<table border="1" class="docutils">
<colgroup>
<col width="23%" />
<col width="14%" />
<col width="42%" />
<col width="21%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td>Scope</td>
<td>Global</td>
<td>Application Root</td>
<td>App Path</td>
</tr>
<tr class="row-even"><td>engine</td>
<td>X</td>
<td> </td>
<td> </td>
</tr>
<tr class="row-odd"><td>hooks</td>
<td>X</td>
<td>X</td>
<td>X</td>
</tr>
<tr class="row-even"><td>log</td>
<td>X</td>
<td>X</td>
<td> </td>
</tr>
<tr class="row-odd"><td>request</td>
<td>X</td>
<td>X</td>
<td>X</td>
</tr>
<tr class="row-even"><td>response</td>
<td>X</td>
<td>X</td>
<td>X</td>
</tr>
<tr class="row-odd"><td>server</td>
<td>X</td>
<td> </td>
<td> </td>
</tr>
<tr class="row-even"><td>tools</td>
<td>X</td>
<td>X</td>
<td>X</td>
</tr>
</tbody>
</table>
<div class="section" id="engine">
<h4>engine<a class="headerlink" href="#engine" title="Permalink to this headline">¶</a></h4>
<p>Entries in this namespace controls the ‘application engine’. These can only be
declared in the global config. Any attribute of
<code class="xref py py-class docutils literal"><span class="pre">cherrypy.engine</span></code> may be set
in config; however, there are a few extra entries available in config:</p>
<blockquote>
<div><ul class="simple">
<li>Plugin attributes. Many of the <span class="xref std std-ref">Engine Plugins</span> are themselves
attributes of <code class="docutils literal"><span class="pre">cherrypy.engine</span></code>. You can set any attribute of an attached
plugin by simply naming it. For example, there is an instance of the
<code class="xref py py-class docutils literal"><span class="pre">Autoreloader</span></code> class at
<code class="docutils literal"><span class="pre">engine.autoreload</span></code>; you can set its “frequency” attribute via the config
entry <code class="docutils literal"><span class="pre">engine.autoreload.frequency</span> <span class="pre">=</span> <span class="pre">60</span></code>. In addition, you can turn such
plugins on and off by setting <code class="docutils literal"><span class="pre">engine.autoreload.on</span> <span class="pre">=</span> <span class="pre">True</span></code> or <code class="docutils literal"><span class="pre">False</span></code>.</li>
<li><code class="docutils literal"><span class="pre">engine.SIGHUP/SIGTERM</span></code>: These entries can be used to set the list of
listeners for the given <span class="xref std std-ref">channel</span>. Mostly, this is used
to turn off the signal handling one gets automatically via
<code class="xref py py-func docutils literal"><span class="pre">cherrypy.quickstart()</span></code>.</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="hooks">
<h4>hooks<a class="headerlink" href="#hooks" title="Permalink to this headline">¶</a></h4>
<p>Declares additional request-processing functions. Use this to append your own
<code class="xref py py-class docutils literal"><span class="pre">Hook</span></code> functions to the request. For example,
to add <code class="docutils literal"><span class="pre">my_hook_func</span></code> to the <code class="docutils literal"><span class="pre">before_handler</span></code> hookpoint:</p>
<div class="highlight-ini"><div class="highlight"><pre><span></span><span class="k">[/]</span>
<span class="na">hooks.before_handler</span> <span class="o">=</span> <span class="s">myapp.my_hook_func</span>
</pre></div>
</div>
</div>
<div class="section" id="log">
<h4>log<a class="headerlink" href="#log" title="Permalink to this headline">¶</a></h4>
<p>Configures logging. These can only be declared in the global config (for global
logging) or <code class="docutils literal"><span class="pre">[/]</span></code> config (for each application).
See <code class="xref py py-class docutils literal"><span class="pre">LogManager</span></code> for the list of
configurable attributes. Typically, the “access_file”, “error_file”, and
“screen” attributes are the most commonly configured.</p>
</div>
<div class="section" id="request">
<h4>request<a class="headerlink" href="#request" title="Permalink to this headline">¶</a></h4>
<p>Sets attributes on each Request. See the
<code class="xref py py-class docutils literal"><span class="pre">Request</span></code> class for a complete list.</p>
</div>
<div class="section" id="response">
<h4>response<a class="headerlink" href="#response" title="Permalink to this headline">¶</a></h4>
<p>Sets attributes on each Response. See the
<code class="xref py py-class docutils literal"><span class="pre">Response</span></code> class for a complete list.</p>
</div>
<div class="section" id="server">
<h4>server<a class="headerlink" href="#server" title="Permalink to this headline">¶</a></h4>
<p>Controls the default HTTP server via
<code class="xref py py-class docutils literal"><span class="pre">cherrypy.server</span></code> (see that class for a
complete list of configurable attributes). These can only be
declared in the global config.</p>
</div>
<div class="section" id="tools">
<h4>tools<a class="headerlink" href="#tools" title="Permalink to this headline">¶</a></h4>
<p>Enables and configures additional request-processing packages. See the
<span class="xref std std-doc">/tutorial/tools</span> overview for more information.</p>
</div>
<div class="section" id="wsgi">
<h4>wsgi<a class="headerlink" href="#wsgi" title="Permalink to this headline">¶</a></h4>
<p>Adds WSGI middleware to an Application’s “pipeline”. These can only be
declared in the app’s root config (“/”).</p>
<blockquote>
<div><ul class="simple">
<li><code class="docutils literal"><span class="pre">wsgi.pipeline</span></code>: Appends to the WSGi pipeline. The value must be a list of
(name, app factory) pairs. Each app factory must be a WSGI callable class
(or callable that returns a WSGI callable); it must take an initial
‘nextapp’ argument, plus any optional keyword arguments. The optional
arguments may be configured via <code class="docutils literal"><span class="pre">wsgi.<name>.<arg></span></code>.</li>
<li><code class="docutils literal"><span class="pre">wsgi.response_class</span></code>: Overrides the default
<code class="xref py py-class docutils literal"><span class="pre">Response</span></code> class.</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="checker">
<h4>checker<a class="headerlink" href="#checker" title="Permalink to this headline">¶</a></h4>
<p>Controls the “checker”, which looks for common errors in app state (including
config) when the engine starts. You can turn off individual checks by setting
them to <code class="docutils literal"><span class="pre">False</span></code> in config. See <code class="xref py py-class docutils literal"><span class="pre">cherrypy._cpchecker.Checker</span></code> for a
complete list. Global config only.</p>
</div>
</div>
<div class="section" id="custom-config-namespaces">
<h3><a class="toc-backref" href="#id13">Custom config namespaces</a><a class="headerlink" href="#custom-config-namespaces" title="Permalink to this headline">¶</a></h3>
<p>You can define your own namespaces if you like, and they can do far more than
simply set attributes. The <code class="docutils literal"><span class="pre">test/test_config</span></code> module, for example, shows an
example of a custom namespace that coerces incoming params and outgoing body
content. The <code class="xref py py-mod docutils literal"><span class="pre">cherrypy._cpwsgi</span></code> module includes an additional, builtin
namespace for invoking WSGI middleware.</p>
<p>In essence, a config namespace handler is just a function, that gets passed
any config entries in its namespace. You add it to a namespaces registry
(a dict), where keys are namespace names and values are handler functions.
When a config entry for your namespace is encountered, the corresponding
handler function will be called, passing the config key and value; that is,
<code class="docutils literal"><span class="pre">namespaces[namespace](k,</span> <span class="pre">v)</span></code>. For example, if you write:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">db_namespace</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
<span class="k">if</span> <span class="n">k</span> <span class="o">==</span> <span class="s1">'connstring'</span><span class="p">:</span>
<span class="n">orm</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">namespaces</span><span class="p">[</span><span class="s1">'db'</span><span class="p">]</span> <span class="o">=</span> <span class="n">db_namespace</span>
</pre></div>
</div>
<p>then <code class="docutils literal"><span class="pre">cherrypy.config.update({"db.connstring":</span> <span class="pre">"Oracle:host=1.10.100.200;sid=TEST"})</span></code>
will call <code class="docutils literal"><span class="pre">db_namespace('connstring',</span> <span class="pre">'Oracle:host=1.10.100.200;sid=TEST')</span></code>.</p>
<p>The point at which your namespace handler is called depends on where you add it:</p>
<table border="1" class="docutils">
<colgroup>
<col width="7%" />
<col width="47%" />
<col width="46%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td>Scope</td>
<td>Namespace dict</td>
<td>Handler is called in</td>
</tr>
<tr class="row-even"><td>Global</td>
<td><code class="xref py py-attr docutils literal"><span class="pre">cherrypy.config.namespaces</span></code></td>
<td>cherrypy.config.update</td>
</tr>
<tr class="row-odd"><td>Application</td>
<td><code class="xref py py-attr docutils literal"><span class="pre">app.namespaces</span></code></td>
<td>Application.merge (which is called by cherrypy.tree.mount)</td>
</tr>
<tr class="row-even"><td>Request</td>
<td><code class="xref py py-attr docutils literal"><span class="pre">app.request_class.namespaces</span></code></td>
<td>Request.configure (called for each request, after the handler is looked up)</td>
</tr>
</tbody>
</table>
<p>The name can be any string, and the handler must be either a callable or a
(Python 2.5 style) context manager.</p>
<p>If you need additional code to run when all your namespace keys are collected,
you can supply a callable context manager in place of a normal function for
the handler. Context managers are defined in <span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0343"><strong>PEP 343</strong></a>.</p>
</div>
<div class="section" id="environments">
<span id="id2"></span><h3><a class="toc-backref" href="#id14">Environments</a><a class="headerlink" href="#environments" title="Permalink to this headline">¶</a></h3>
<p>The only key that does not exist in a namespace is the <em>“environment”</em> entry.
It only applies to the global config, and only when you use
<code class="xref py py-func docutils literal"><span class="pre">cherrypy.config.update</span></code>. This special
entry <em>imports</em> other config entries from the following template stored in
<code class="docutils literal"><span class="pre">cherrypy._cpconfig.environments[environment]</span></code>.</p>
<p>If you find the set of existing environments (production, staging, etc) too
limiting or just plain wrong, feel free to extend them or add new environments:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">cherrypy</span><span class="o">.</span><span class="n">_cpconfig</span><span class="o">.</span><span class="n">environments</span><span class="p">[</span><span class="s1">'staging'</span><span class="p">][</span><span class="s1">'log.screen'</span><span class="p">]</span> <span class="o">=</span> <span class="bp">False</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">_cpconfig</span><span class="o">.</span><span class="n">environments</span><span class="p">[</span><span class="s1">'Greek'</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'tools.encode.encoding'</span><span class="p">:</span> <span class="s1">'ISO-8859-7'</span><span class="p">,</span>
<span class="s1">'tools.decode.encoding'</span><span class="p">:</span> <span class="s1">'ISO-8859-7'</span><span class="p">,</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Configure</a><ul>
<li><a class="reference internal" href="#architecture">Architecture</a><ul>
<li><a class="reference internal" href="#global-config">Global config</a></li>
<li><a class="reference internal" href="#application-config">Application config</a></li>
<li><a class="reference internal" href="#request-config">Request config</a></li>
</ul>
</li>
<li><a class="reference internal" href="#declaration">Declaration</a><ul>
<li><a class="reference internal" href="#configuration-files">Configuration files</a><ul>
<li><a class="reference internal" href="#combined-configuration-files">Combined Configuration Files</a></li>
<li><a class="reference internal" href="#separate-configuration-files">Separate Configuration Files</a></li>
<li><a class="reference internal" href="#values-in-config-files-use-python-syntax">Values in config files use Python syntax</a></li>
</ul>
</li>
<li><a class="reference internal" href="#cp-config-attaching-config-to-handlers">_cp_config: attaching config to handlers</a></li>
</ul>
</li>
<li><a class="reference internal" href="#namespaces">Namespaces</a><ul>
<li><a class="reference internal" href="#builtin-namespaces">Builtin namespaces</a><ul>
<li><a class="reference internal" href="#engine">engine</a></li>
<li><a class="reference internal" href="#hooks">hooks</a></li>
<li><a class="reference internal" href="#log">log</a></li>
<li><a class="reference internal" href="#request">request</a></li>
<li><a class="reference internal" href="#response">response</a></li>
<li><a class="reference internal" href="#server">server</a></li>
<li><a class="reference internal" href="#tools">tools</a></li>
<li><a class="reference internal" href="#wsgi">wsgi</a></li>
<li><a class="reference internal" href="#checker">checker</a></li>
</ul>
</li>
<li><a class="reference internal" href="#custom-config-namespaces">Custom config namespaces</a></li>
<li><a class="reference internal" href="#environments">Environments</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="advanced.html"
title="previous chapter">Advanced</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="extend.html"
title="next chapter">Extend</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/config.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="extend.html" title="Extend"
>next</a> |</li>
<li class="right" >
<a href="advanced.html" title="Advanced"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">CherryPy 8.9.1 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2001-2018 CherryPy Team.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.6.
</div>
</body>
</html>
|