/usr/share/doc/python-cherrypy3-doc/html/extend.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 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 | <!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>Extend — 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="Deploy" href="deploy.html" />
<link rel="prev" title="Configure" href="config.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="deploy.html" title="Deploy"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="config.html" title="Configure"
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="extend">
<span id="id1"></span><h1><a class="toc-backref" href="#id5">Extend</a><a class="headerlink" href="#extend" title="Permalink to this headline">¶</a></h1>
<p>CherryPy is truly an open framework, you can extend and plug
new functions at will either server-side or on a per-requests basis.
Either way, CherryPy is made to help you build your
application and support your architecture via simple patterns.</p>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#extend" id="id5">Extend</a><ul>
<li><a class="reference internal" href="#server-wide-functions" id="id6">Server-wide functions</a><ul>
<li><a class="reference internal" href="#publish-subscribe-pattern" id="id7">Publish/Subscribe pattern</a><ul>
<li><a class="reference internal" href="#typical-pattern" id="id8">Typical pattern</a></li>
<li><a class="reference internal" href="#implementation-details" id="id9">Implementation details</a></li>
<li><a class="reference internal" href="#engine-as-a-pubsub-bus" id="id10">Engine as a pubsub bus</a></li>
<li><a class="reference internal" href="#built-in-channels" id="id11">Built-in channels</a></li>
<li><a class="reference internal" href="#bus-api" id="id12">Bus API</a></li>
</ul>
</li>
<li><a class="reference internal" href="#plugins" id="id13">Plugins</a><ul>
<li><a class="reference internal" href="#create-a-plugin" id="id14">Create a plugin</a></li>
<li><a class="reference internal" href="#enable-a-plugin" id="id15">Enable a plugin</a></li>
<li><a class="reference internal" href="#disable-a-plugin" id="id16">Disable a plugin</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#per-request-functions" id="id17">Per-request functions</a><ul>
<li><a class="reference internal" href="#hook-point" id="id18">Hook point</a></li>
<li><a class="reference internal" href="#tools" id="id19">Tools</a><ul>
<li><a class="reference internal" href="#stateful-tools" id="id20">Stateful tools</a></li>
<li><a class="reference internal" href="#tools-ordering" id="id21">Tools ordering</a></li>
<li><a class="reference internal" href="#toolboxes" id="id22">Toolboxes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#request-parameters-manipulation" id="id23">Request parameters manipulation</a></li>
</ul>
</li>
<li><a class="reference internal" href="#tailored-dispatchers" id="id24">Tailored dispatchers</a><ul>
<li><a class="reference internal" href="#tool-or-dispatcher" id="id25">Tool or dispatcher?</a></li>
</ul>
</li>
<li><a class="reference internal" href="#request-body-processors" id="id26">Request body processors</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="server-wide-functions">
<h2><a class="toc-backref" href="#id6">Server-wide functions</a><a class="headerlink" href="#server-wide-functions" title="Permalink to this headline">¶</a></h2>
<p>CherryPy can be considered both as a HTTP library
as much as a web application framework. In that latter case,
its architecture provides mechanisms to support operations
accross the whole server instance. This offers a powerful
canvas to perform persistent operations as server-wide
functions live outside the request processing itself. They
are available to the whole process as long as the bus lives.</p>
<p>Typical use cases:</p>
<ul class="simple">
<li>Keeping a pool of connection to an external server so that
your need not to re-open them on each request (database connections
for instance).</li>
<li>Background processing (say you need work to be done without
blocking the whole request itself).</li>
</ul>
<div class="section" id="publish-subscribe-pattern">
<h3><a class="toc-backref" href="#id7">Publish/Subscribe pattern</a><a class="headerlink" href="#publish-subscribe-pattern" title="Permalink to this headline">¶</a></h3>
<p>CherryPy’s backbone consists of a bus system implementing
a simple <a class="reference external" href="http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern">publish/subscribe messaging pattern</a>.
Simply put, in CherryPy everything is controlled via that bus.
One can easily picture the bus as a sushi restaurant’s belt as in
the picture below.</p>
<a class="reference external image-reference" href="http://en.wikipedia.org/wiki/YO!_Sushi"><img alt="_images/sushibelt.JPG" src="_images/sushibelt.JPG" /></a>
<p>You can subscribe and publish to channels on a bus. A channel is
bit like a unique identifier within the bus. When a message is
published to a channel, the bus will dispatch the message to
all subscribers for that channel.</p>
<p>One interesting aspect of a pubsub pattern is that it promotes
decoupling between a caller and the callee. A published message
will eventually generate a response but the publisher does not
know where that response came from.</p>
<p>Thanks to that decoupling, a CherryPy application can easily
access functionalities without having to hold a reference to
the entity providing that functionality. Instead, the
application simply publishes onto the bus and will receive
the appropriate response, which is all that matter.</p>
<div class="section" id="typical-pattern">
<span id="buspattern"></span><h4><a class="toc-backref" href="#id8">Typical pattern</a><a class="headerlink" href="#typical-pattern" title="Permalink to this headline">¶</a></h4>
<p>Let’s take the following dummy application:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cherrypy</span>
<span class="k">class</span> <span class="nc">ECommerce</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">mydb</span> <span class="o">=</span> <span class="n">db</span>
<span class="nd">@cherrypy.expose</span>
<span class="k">def</span> <span class="nf">save_kart</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cart_data</span><span class="p">):</span>
<span class="n">cart</span> <span class="o">=</span> <span class="n">Cart</span><span class="p">(</span><span class="n">cart_data</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">mydb</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">cart</span><span class="p">)</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">quickstart</span><span class="p">(</span><span class="n">ECommerce</span><span class="p">(),</span> <span class="s1">'/'</span><span class="p">)</span>
</pre></div>
</div>
<p>The application has a reference to the database but
this creates a fairly strong coupling between the
database provider and the application.</p>
<p>Another approach to work around the coupling is by
using a pubsub workflow:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cherrypy</span>
<span class="k">class</span> <span class="nc">ECommerce</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@cherrypy.expose</span>
<span class="k">def</span> <span class="nf">save_kart</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cart_data</span><span class="p">):</span>
<span class="n">cart</span> <span class="o">=</span> <span class="n">Cart</span><span class="p">(</span><span class="n">cart_data</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">publish</span><span class="p">(</span><span class="s1">'db-save'</span><span class="p">,</span> <span class="n">cart</span><span class="p">)</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">quickstart</span><span class="p">(</span><span class="n">ECommerce</span><span class="p">(),</span> <span class="s1">'/'</span><span class="p">)</span>
</pre></div>
</div>
<p>In this example, we publish a <cite>cart</cite> instance to
<cite>db-save</cite> channel. One or many subscribers can then
react to that message and the application doesn’t
have to know about them.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This approach is not mandatory and it’s up to you to
decide how to design your entities interaction.</p>
</div>
</div>
<div class="section" id="implementation-details">
<h4><a class="toc-backref" href="#id9">Implementation details</a><a class="headerlink" href="#implementation-details" title="Permalink to this headline">¶</a></h4>
<p>CherryPy’s bus implementation is simplistic as it registers
functions to channels. Whenever a message is published to
a channel, each registered function is applied with that
message passed as a parameter.</p>
<p>The whole behaviour happens synchronously and, in that sense,
if a subscriber takes too long to process a message, the
remaining subscribers will be delayed.</p>
<p>CherryPy’s bus is not an advanced pubsub messaging broker
system such as provided by <a class="reference external" href="http://zeromq.org/">zeromq</a> or
<a class="reference external" href="https://www.rabbitmq.com/">RabbitMQ</a>.
Use it with the understanding that it may have a cost.</p>
</div>
<div class="section" id="engine-as-a-pubsub-bus">
<span id="cpengine"></span><h4><a class="toc-backref" href="#id10">Engine as a pubsub bus</a><a class="headerlink" href="#engine-as-a-pubsub-bus" title="Permalink to this headline">¶</a></h4>
<p>As said earlier, CherryPy is built around a pubsub bus. All
entities that the framework manages at runtime are working on
top of a single bus instance, which is named the <cite>engine</cite>.</p>
<p>The bus implementation therefore provides a set of common
channels which describe the application’s lifecycle:</p>
<div class="highlight-text"><div class="highlight"><pre><span></span> O
|
V
STOPPING --> STOPPED --> EXITING -> X
A A |
| \___ |
| \ |
| V V
STARTED <-- STARTING
</pre></div>
</div>
<p>The states’ transitions trigger channels to be published
to so that subscribers can react to them.</p>
<p>One good example is the HTTP server which will tranisition
from a <cite>“STOPPED”</cite> stated to a <cite>“STARTED”</cite> state whenever
a message is published to the <cite>start</cite> channel.</p>
</div>
<div class="section" id="built-in-channels">
<h4><a class="toc-backref" href="#id11">Built-in channels</a><a class="headerlink" href="#built-in-channels" title="Permalink to this headline">¶</a></h4>
<p>In order to support its life-cycle, CherryPy defines a set
of common channels that will be published to at various states:</p>
<ul class="simple">
<li><strong>“start”</strong>: When the bus is in the <cite>“STARTING”</cite> state</li>
<li><strong>“main”</strong>: Periodically from the CherryPy’s mainloop</li>
<li><strong>“stop”</strong>: When the bus is in the <cite>“STOPPING”</cite> state</li>
<li><strong>“graceful”</strong>: When the bus requests a reload of subscribers</li>
<li><strong>“exit”</strong>: When the bus is in the <cite>“EXITING”</cite> state</li>
</ul>
<p>This channel will be published to by the <cite>engine</cite> automatically.
Register therefore any subscribers that would need to react
to the transition changes of the <cite>engine</cite>.</p>
<p>In addition, a few other channels are also published to during
the request processing.</p>
<ul class="simple">
<li><a href="#id2"><span class="problematic" id="id3">`</span></a><strong>”before_request”</strong>: right before the request is processed by CherryPy</li>
<li><strong>“after_request”</strong>: right after it has been processed</li>
</ul>
<p>Also, from the <code class="xref py py-class docutils literal"><span class="pre">cherrypy.process.plugins.ThreadManager</span></code> plugin:</p>
<ul class="simple">
<li><strong>“acquire_thread”</strong></li>
<li><strong>“start_thread”</strong></li>
<li><strong>“stop_thread”</strong></li>
<li><strong>“release_thread”</strong></li>
</ul>
</div>
<div class="section" id="bus-api">
<h4><a class="toc-backref" href="#id12">Bus API</a><a class="headerlink" href="#bus-api" title="Permalink to this headline">¶</a></h4>
<p>In order to work with the bus, the implementation
provides the following simple API:</p>
<ul class="simple">
<li><code class="xref py py-meth docutils literal"><span class="pre">cherrypy.engine.publish(channel,</span> <span class="pre">*args)</span></code>:</li>
</ul>
<blockquote>
<div><ul class="simple">
<li>The <cite>channel</cite> parameter is a string identifying the channel to
which the message should be sent to</li>
<li><cite>*args</cite> is the message and may contain any valid Python values or
objects.</li>
</ul>
</div></blockquote>
<ul class="simple">
<li><code class="xref py py-meth docutils literal"><span class="pre">cherrypy.engine.subscribe(channel,</span> <span class="pre">callable)</span></code>:</li>
</ul>
<blockquote>
<div><ul class="simple">
<li>The <cite>channel</cite> parameter is a string identifying the channel the
<cite>callable</cite> will be registered to.</li>
<li><cite>callable</cite> is a Python function or method which signature must
match what will be published.</li>
</ul>
</div></blockquote>
<ul class="simple">
<li><code class="xref py py-meth docutils literal"><span class="pre">cherrypy.engine.unsubscribe(channel,</span> <span class="pre">callable)</span></code>:</li>
</ul>
<blockquote>
<div><ul class="simple">
<li>The <cite>channel</cite> parameter is a string identifying the channel the
<cite>callable</cite> was registered to.</li>
<li><cite>callable</cite> is the Python function or method which was registered.</li>
</ul>
</div></blockquote>
</div>
</div>
<div class="section" id="plugins">
<span id="busplugins"></span><h3><a class="toc-backref" href="#id13">Plugins</a><a class="headerlink" href="#plugins" title="Permalink to this headline">¶</a></h3>
<p>Plugins, simply put, are entities that play with the bus, either by
publishing or subscribing to channels, usually both at the same time.</p>
<div class="admonition important">
<p class="first admonition-title">Important</p>
<p>Plugins are extremely useful whenever you have functionalities:</p>
<ul class="last simple">
<li>Available accross the whole application server</li>
<li>Associated to the application’s life-cycle</li>
<li>You want to avoid being strongly coupled to the application</li>
</ul>
</div>
<div class="section" id="create-a-plugin">
<h4><a class="toc-backref" href="#id14">Create a plugin</a><a class="headerlink" href="#create-a-plugin" title="Permalink to this headline">¶</a></h4>
<p>A typical plugin looks like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cherrypy</span>
<span class="kn">from</span> <span class="nn">cherrypy.process</span> <span class="kn">import</span> <span class="n">wspbus</span><span class="p">,</span> <span class="n">plugins</span>
<span class="k">class</span> <span class="nc">DatabasePlugin</span><span class="p">(</span><span class="n">plugins</span><span class="o">.</span><span class="n">SimplePlugin</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bus</span><span class="p">,</span> <span class="n">db_klass</span><span class="p">):</span>
<span class="n">plugins</span><span class="o">.</span><span class="n">SimplePlugin</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bus</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="n">db_klass</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">bus</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s1">'Starting up DB access'</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">bus</span><span class="o">.</span><span class="n">subscribe</span><span class="p">(</span><span class="s2">"db-save"</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">save_it</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">bus</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s1">'Stopping down DB access'</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">bus</span><span class="o">.</span><span class="n">unsubscribe</span><span class="p">(</span><span class="s2">"db-save"</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">save_it</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">save_it</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entity</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">entity</span><span class="p">)</span>
</pre></div>
</div>
<p>The <code class="xref py py-class docutils literal"><span class="pre">cherrypy.process.plugins.SimplePlugin</span></code> is a helper
class provided by CherryPy that will automatically subscribe
your <cite>start</cite> and <cite>stop</cite> methods to the related channels.</p>
<p>When the <cite>start</cite> and <cite>stop</cite> channels are published on, those
methods are called accordingly.</p>
<p>Notice then how our plugin subscribes to the <cite>db-save</cite>
channel so that the bus can dispatch messages to the plugin.</p>
</div>
<div class="section" id="enable-a-plugin">
<h4><a class="toc-backref" href="#id15">Enable a plugin</a><a class="headerlink" href="#enable-a-plugin" title="Permalink to this headline">¶</a></h4>
<p>To enable the plugin, it has to be registered to the the
bus as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">DatabasePlugin</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="n">SQLiteDB</span><span class="p">)</span><span class="o">.</span><span class="n">subscribe</span><span class="p">()</span>
</pre></div>
</div>
<p>The <cite>SQLiteDB</cite> here is a fake class that is used as our
database provider.</p>
</div>
<div class="section" id="disable-a-plugin">
<h4><a class="toc-backref" href="#id16">Disable a plugin</a><a class="headerlink" href="#disable-a-plugin" title="Permalink to this headline">¶</a></h4>
<p>You can also unregister a plugin as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">someplugin</span><span class="o">.</span><span class="n">unsubscribe</span><span class="p">()</span>
</pre></div>
</div>
<p>This is often used when you want to prevent the default
HTTP server from being started by CherryPy, for instance
if you run on top of a different HTTP server (WSGI capable):</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">cherrypy</span><span class="o">.</span><span class="n">server</span><span class="o">.</span><span class="n">unsubscribe</span><span class="p">()</span>
</pre></div>
</div>
<p>Let’s see an example using this default application:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cherrypy</span>
<span class="k">class</span> <span class="nc">Root</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@cherrypy.expose</span>
<span class="k">def</span> <span class="nf">index</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="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">quickstart</span><span class="p">(</span><span class="n">Root</span><span class="p">())</span>
</pre></div>
</div>
<p>For instance, this is what you would see when running
this application:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mo">04</span><span class="p">:</span><span class="mo">07</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Listening</span> <span class="k">for</span> <span class="n">SIGHUP</span><span class="o">.</span>
<span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mo">04</span><span class="p">:</span><span class="mo">07</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Listening</span> <span class="k">for</span> <span class="n">SIGTERM</span><span class="o">.</span>
<span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mo">04</span><span class="p">:</span><span class="mo">07</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Listening</span> <span class="k">for</span> <span class="n">SIGUSR1</span><span class="o">.</span>
<span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mo">04</span><span class="p">:</span><span class="mo">07</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Bus</span> <span class="n">STARTING</span>
<span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mo">04</span><span class="p">:</span><span class="mo">07</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Started</span> <span class="n">monitor</span> <span class="n">thread</span> <span class="s1">'Autoreloader'</span><span class="o">.</span>
<span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mo">04</span><span class="p">:</span><span class="mo">07</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Started</span> <span class="n">monitor</span> <span class="n">thread</span> <span class="s1">'_TimeoutMonitor'</span><span class="o">.</span>
<span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mo">04</span><span class="p">:</span><span class="mi">08</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Serving</span> <span class="n">on</span> <span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="p">:</span><span class="mi">8080</span>
<span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mo">04</span><span class="p">:</span><span class="mi">08</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Bus</span> <span class="n">STARTED</span>
</pre></div>
</div>
<p>Now let’s unsubscribe the HTTP server:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cherrypy</span>
<span class="k">class</span> <span class="nc">Root</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@cherrypy.expose</span>
<span class="k">def</span> <span class="nf">index</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="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">server</span><span class="o">.</span><span class="n">unsubscribe</span><span class="p">()</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">quickstart</span><span class="p">(</span><span class="n">Root</span><span class="p">())</span>
</pre></div>
</div>
<p>This is what we get:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mi">08</span><span class="p">:</span><span class="mo">06</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Listening</span> <span class="k">for</span> <span class="n">SIGHUP</span><span class="o">.</span>
<span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mi">08</span><span class="p">:</span><span class="mo">06</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Listening</span> <span class="k">for</span> <span class="n">SIGTERM</span><span class="o">.</span>
<span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mi">08</span><span class="p">:</span><span class="mo">06</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Listening</span> <span class="k">for</span> <span class="n">SIGUSR1</span><span class="o">.</span>
<span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mi">08</span><span class="p">:</span><span class="mo">06</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Bus</span> <span class="n">STARTING</span>
<span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mi">08</span><span class="p">:</span><span class="mo">06</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Started</span> <span class="n">monitor</span> <span class="n">thread</span> <span class="s1">'Autoreloader'</span><span class="o">.</span>
<span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mi">08</span><span class="p">:</span><span class="mo">06</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Started</span> <span class="n">monitor</span> <span class="n">thread</span> <span class="s1">'_TimeoutMonitor'</span><span class="o">.</span>
<span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mi">08</span><span class="p">:</span><span class="mo">06</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Bus</span> <span class="n">STARTED</span>
</pre></div>
</div>
<p>As you can see, the server is not started. The missing:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="p">[</span><span class="mi">27</span><span class="o">/</span><span class="n">Apr</span><span class="o">/</span><span class="mi">2014</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mo">04</span><span class="p">:</span><span class="mi">08</span><span class="p">]</span> <span class="n">ENGINE</span> <span class="n">Serving</span> <span class="n">on</span> <span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="mf">127.0</span><span class="o">.</span><span class="mf">0.1</span><span class="p">:</span><span class="mi">8080</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="per-request-functions">
<h2><a class="toc-backref" href="#id17">Per-request functions</a><a class="headerlink" href="#per-request-functions" title="Permalink to this headline">¶</a></h2>
<p>One of the most common task in a web application development
is to tailor the request’s processing to the runtime context.</p>
<p>Within CherryPy, this is performed via what are called <cite>tools</cite>.
If you are familiar with Django or WSGI middlewares,
CherryPy tools are similar in spirit.
They add functions that are applied during the
request/response processing.</p>
<div class="section" id="hook-point">
<span id="hookpoint"></span><h3><a class="toc-backref" href="#id18">Hook point</a><a class="headerlink" href="#hook-point" title="Permalink to this headline">¶</a></h3>
<p>A hook point is a point during the request/response processing.</p>
<p>Here is a quick rundown of the “hook points” that you can hang your tools on:</p>
<blockquote>
<div><ul class="simple">
<li><strong>“on_start_resource”</strong> - The earliest hook; the Request-Line and request headers
have been processed and a dispatcher has set request.handler and request.config.</li>
<li><strong>“before_request_body”</strong> - Tools that are hooked up here run right before the
request body would be processed.</li>
<li><strong>“before_handler”</strong> - Right before the request.handler (the <a class="reference internal" href="glossary.html#term-exposed"><span class="xref std std-term">exposed</span></a> callable
that was found by the dispatcher) is called.</li>
<li><strong>“before_finalize”</strong> - This hook is called right after the page handler has been
processed and before CherryPy formats the final response object. It helps
you for example to check for what could have been returned by your page
handler and change some headers if needed.</li>
<li><strong>“on_end_resource”</strong> - Processing is complete - the response is ready to be
returned. This doesn’t always mean that the request.handler (the exposed
page handler) has executed! It may be a generator. If your tool absolutely
needs to run after the page handler has produced the response body, you
need to either use on_end_request instead, or wrap the response.body in a
generator which applies your tool as the response body is being generated.</li>
<li><strong>“before_error_response”</strong> - Called right before an error response
(status code, body) is set.</li>
<li><strong>“after_error_response”</strong> - Called right after the error response
(status code, body) is set and just before the error response is finalized.</li>
<li><strong>“on_end_request”</strong> - The request/response conversation is over, all data has
been written to the client, nothing more to see here, move along.</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="tools">
<span id="id4"></span><h3><a class="toc-backref" href="#id19">Tools</a><a class="headerlink" href="#tools" title="Permalink to this headline">¶</a></h3>
<p>A tool is a simple callable object (function, method, object
implementing a <cite>__call__</cite> method) that is attached to a
<a class="reference internal" href="#hookpoint"><span class="std std-ref">hook point</span></a>.</p>
<p>Below is a simple tool that is attached to the <cite>before_finalize</cite>
hook point, hence after the page handler was called:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="nd">@cherrypy.tools.register</span><span class="p">(</span><span class="s1">'before_finalize'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">logit</span><span class="p">():</span>
<span class="k">print</span><span class="p">(</span><span class="n">cherrypy</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">remote</span><span class="o">.</span><span class="n">ip</span><span class="p">)</span>
</pre></div>
</div>
<p>Tools can also be created and assigned manually.
The decorator registration is equivalent to:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">cherrypy</span><span class="o">.</span><span class="n">tools</span><span class="o">.</span><span class="n">logit</span> <span class="o">=</span> <span class="n">cherrypy</span><span class="o">.</span><span class="n">Tool</span><span class="p">(</span><span class="s1">'before_finalize'</span><span class="p">,</span> <span class="n">logit</span><span class="p">)</span>
</pre></div>
</div>
<p>Using that tool is as simple as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Root</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@cherrypy.expose</span>
<span class="nd">@cherrypy.tools.logit</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">index</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>
</pre></div>
</div>
<p>Obviously the tool may be declared the
<a class="reference internal" href="basics.html#perappconf"><span class="std std-ref">other usual ways</span></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The name of the tool, technically the attribute set to <cite>cherrypy.tools</cite>,
does not have to match the name of the callable. However, it is
that name that will be used in the configuration to refer to that
tool.</p>
</div>
<div class="section" id="stateful-tools">
<h4><a class="toc-backref" href="#id20">Stateful tools</a><a class="headerlink" href="#stateful-tools" title="Permalink to this headline">¶</a></h4>
<p>The tools mechanism is really flexible and enables
rich per-request functionalities.</p>
<p>Straight tools as shown in the previous section are
usually good enough. However, if your workflow
requires some sort of state during the request processing,
you will probably want a class-based approach:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">cherrypy</span>
<span class="k">class</span> <span class="nc">TimingTool</span><span class="p">(</span><span class="n">cherrypy</span><span class="o">.</span><span class="n">Tool</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">Tool</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">'before_handler'</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">start_timer</span><span class="p">,</span>
<span class="n">priority</span><span class="o">=</span><span class="mi">95</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">_setup</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">Tool</span><span class="o">.</span><span class="n">_setup</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">hooks</span><span class="o">.</span><span class="n">attach</span><span class="p">(</span><span class="s1">'before_finalize'</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">end_timer</span><span class="p">,</span>
<span class="n">priority</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">start_timer</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">end_timer</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">duration</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">cherrypy</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">_time</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="s2">"Page handler took </span><span class="si">%.4f</span><span class="s2">"</span> <span class="o">%</span> <span class="n">duration</span><span class="p">)</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">tools</span><span class="o">.</span><span class="n">timeit</span> <span class="o">=</span> <span class="n">TimingTool</span><span class="p">()</span>
</pre></div>
</div>
<p>This tool computes the time taken by the page handler
for a given request. It stores the time at which the handler
is about to get called and logs the time difference
right after the handler returned its result.</p>
<p>The import bits is that the <code class="xref py py-class docutils literal"><span class="pre">cherrypy.Tool</span></code> constructor
allows you to register to a hook point but, to attach the
same tool to a different hook point, you must use the
<code class="xref py py-meth docutils literal"><span class="pre">cherrypy.request.hooks.attach</span></code> method.
The <code class="xref py py-meth docutils literal"><span class="pre">cherrypy.Tool._setup</span></code>
method is automatically called by CherryPy when the tool
is applied to the request.</p>
<p>Next, let’s see how to use our tool:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Root</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@cherrypy.expose</span>
<span class="nd">@cherrypy.tools.timeit</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">index</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>
</pre></div>
</div>
</div>
<div class="section" id="tools-ordering">
<h4><a class="toc-backref" href="#id21">Tools ordering</a><a class="headerlink" href="#tools-ordering" title="Permalink to this headline">¶</a></h4>
<p>Since you can register many tools at the same hookpoint,
you may wonder in which order they will be applied.</p>
<p>CherryPy offers a deterministic, yet so simple, mechanism
to do so. Simply set the <strong>priority</strong> attribute to a value
from 1 to 100, lower values providing greater priority.</p>
<p>If you set the same priority for several tools, they will
be called in the order you declare them in your configuration.</p>
</div>
<div class="section" id="toolboxes">
<h4><a class="toc-backref" href="#id22">Toolboxes</a><a class="headerlink" href="#toolboxes" title="Permalink to this headline">¶</a></h4>
<p>All of the builtin CherryPy tools are collected into a Toolbox called
<code class="xref py py-attr docutils literal"><span class="pre">cherrypy.tools</span></code>. It responds to config entries in the <code class="docutils literal"><span class="pre">"tools"</span></code>
<a class="reference internal" href="config.html#namespaces"><span class="std std-ref">namespace</span></a>. You can add your own Tools to this Toolbox
as described above.</p>
<p>You can also make your own Toolboxes if you need more modularity. For example,
you might create multiple Tools for working with JSON, or you might publish
a set of Tools covering authentication and authorization from which everyone
could benefit (hint, hint). Creating a new Toolbox is as simple as:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cherrypy</span>
<span class="c1"># Create a new Toolbox.</span>
<span class="n">newauthtools</span> <span class="o">=</span> <span class="n">cherrypy</span><span class="o">.</span><span class="n">_cptools</span><span class="o">.</span><span class="n">Toolbox</span><span class="p">(</span><span class="s2">"newauth"</span><span class="p">)</span>
<span class="c1"># Add a Tool to our new Toolbox.</span>
<span class="nd">@newauthtools.register</span><span class="p">(</span><span class="s1">'before_request_body'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">check_access</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">cherrypy</span><span class="o">.</span><span class="n">request</span><span class="p">,</span> <span class="s2">"userid"</span><span class="p">,</span> <span class="n">default</span><span class="p">):</span>
<span class="k">raise</span> <span class="n">cherrypy</span><span class="o">.</span><span class="n">HTTPError</span><span class="p">(</span><span class="mi">401</span><span class="p">)</span>
</pre></div>
</div>
<p>Then, in your application, use it just like you would use <code class="docutils literal"><span class="pre">cherrypy.tools</span></code>,
with the additional step of registering your toolbox with your app.
Note that doing so automatically registers the <code class="docutils literal"><span class="pre">"newauth"</span></code> config namespace;
you can see the config entries in action below:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cherrypy</span>
<span class="k">class</span> <span class="nc">Root</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@cherrypy.expose</span>
<span class="k">def</span> <span class="nf">default</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">"Hello"</span>
<span class="n">conf</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'/demo'</span><span class="p">:</span> <span class="p">{</span>
<span class="s1">'newauth.check_access.on'</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="s1">'newauth.check_access.default'</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n">app</span> <span class="o">=</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">conf</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="request-parameters-manipulation">
<h3><a class="toc-backref" href="#id23">Request parameters manipulation</a><a class="headerlink" href="#request-parameters-manipulation" title="Permalink to this headline">¶</a></h3>
<p>HTTP uses strings to carry data between two endpoints.
However your application may make better use of richer
object types. As it wouldn’t be really readable, nor
a good idea regarding maintenance, to let each page handler
deserialize data, it’s a common pattern to delegate
this functions to tools.</p>
<p>For instance, let’s assume you have a user id in the query-string
and some user data stored into a database. You could
retrieve the data, create an object and pass it on to the
page handler instead of the user id.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cherrypy</span>
<span class="k">class</span> <span class="nc">UserManager</span><span class="p">(</span><span class="n">cherrypy</span><span class="o">.</span><span class="n">Tool</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">Tool</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">'before_handler'</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">load</span><span class="p">,</span> <span class="n">priority</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">load</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">req</span> <span class="o">=</span> <span class="n">cherrypy</span><span class="o">.</span><span class="n">request</span>
<span class="c1"># let's assume we have a db session</span>
<span class="c1"># attached to the request somehow</span>
<span class="n">db</span> <span class="o">=</span> <span class="n">req</span><span class="o">.</span><span class="n">db</span>
<span class="c1"># retrieve the user id and remove it</span>
<span class="c1"># from the request parameters</span>
<span class="n">user_id</span> <span class="o">=</span> <span class="n">req</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">'user_id'</span><span class="p">)</span>
<span class="n">req</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">'user'</span><span class="p">]</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">user_id</span><span class="p">))</span>
<span class="n">cherrypy</span><span class="o">.</span><span class="n">tools</span><span class="o">.</span><span class="n">user</span> <span class="o">=</span> <span class="n">UserManager</span><span class="p">()</span>
<span class="k">class</span> <span class="nc">Root</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@cherrypy.expose</span>
<span class="nd">@cherrypy.tools.user</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">user</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">"hello </span><span class="si">%s</span><span class="s2">"</span> <span class="o">%</span> <span class="n">user</span><span class="o">.</span><span class="n">name</span>
</pre></div>
</div>
<p>In other words, CherryPy give you the power to:</p>
<ul class="simple">
<li>inject data, that wasn’t part of the initial request, into the page handler</li>
<li>remove data as well</li>
<li>convert data into a different, more useful, object to remove that burden
from the page handler itself</li>
</ul>
</div>
</div>
<div class="section" id="tailored-dispatchers">
<span id="dispatchers"></span><h2><a class="toc-backref" href="#id24">Tailored dispatchers</a><a class="headerlink" href="#tailored-dispatchers" title="Permalink to this headline">¶</a></h2>
<p>Dispatching is the art of locating the appropriate page handler
for a given request. Usually, dispatching is based on the
request’s URL, the query-string and, sometimes, the request’s method
(GET, POST, etc.).</p>
<p>Based on this, CherryPy comes with various dispatchers already.</p>
<p>In some cases however, you will need a little more. Here is an example
of dispatcher that will always ensure the incoming URL leads
to a lower-case page handler.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">random</span>
<span class="kn">import</span> <span class="nn">string</span>
<span class="kn">import</span> <span class="nn">cherrypy</span>
<span class="kn">from</span> <span class="nn">cherrypy._cpdispatch</span> <span class="kn">import</span> <span class="n">Dispatcher</span>
<span class="k">class</span> <span class="nc">StringGenerator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@cherrypy.expose</span>
<span class="k">def</span> <span class="nf">generate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">length</span><span class="o">=</span><span class="mi">8</span><span class="p">):</span>
<span class="k">return</span> <span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">hexdigits</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">length</span><span class="p">)))</span>
<span class="k">class</span> <span class="nc">ForceLowerDispatcher</span><span class="p">(</span><span class="n">Dispatcher</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path_info</span><span class="p">):</span>
<span class="k">return</span> <span class="n">Dispatcher</span><span class="o">.</span><span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path_info</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
<span class="n">conf</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">ForceLowerDispatcher</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">quickstart</span><span class="p">(</span><span class="n">StringGenerator</span><span class="p">(),</span> <span class="s1">'/'</span><span class="p">,</span> <span class="n">conf</span><span class="p">)</span>
</pre></div>
</div>
<p>Once you run this snipper, go to:</p>
<ul class="simple">
<li><a class="reference external" href="http://localhost:8080/generate?length=8">http://localhost:8080/generate?length=8</a></li>
<li><a class="reference external" href="http://localhost:8080/GENerAte?length=8">http://localhost:8080/GENerAte?length=8</a></li>
</ul>
<p>In both cases, you will be led to the <cite>generate</cite> page
handler. Without our home-made dispatcher, the second
one would fail and return a 404 error (<span class="target" id="index-0"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2616.html#sec10.4.5"><strong>RFC 2616#sec10.4.5</strong></a>).</p>
<div class="section" id="tool-or-dispatcher">
<h3><a class="toc-backref" href="#id25">Tool or dispatcher?</a><a class="headerlink" href="#tool-or-dispatcher" title="Permalink to this headline">¶</a></h3>
<p>In the previous example, why not simply use a tool? Well, the sooner
a tool can be called is always after the page handler has been found.
In our example, it would be already too late as the default dispatcher
would have not even found a match for <cite>/GENerAte</cite>.</p>
<p>A dispatcher exists mostly to determine the best page
handler to serve the requested resource.</p>
<p>On ther other hand, tools are there to adapt the request’s processing
to the runtime context of the application and the request’s content.</p>
<p>Usually, you will have to write a dispatcher only if you
have a very specific use case to locate the most adequate
page handler. Otherwise, the default ones will likely suffice.</p>
</div>
</div>
<div class="section" id="request-body-processors">
<h2><a class="toc-backref" href="#id26">Request body processors</a><a class="headerlink" href="#request-body-processors" title="Permalink to this headline">¶</a></h2>
<p>Since its 3.2 release, CherryPy provides a really elegant
and powerful mechanism to deal with a request’s body based
on its mimetype. Refer to the <code class="xref py py-mod docutils literal"><span class="pre">cherrypy._cpreqbody</span></code> module
to understand how to implement your own processors.</p>
</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="#">Extend</a><ul>
<li><a class="reference internal" href="#server-wide-functions">Server-wide functions</a><ul>
<li><a class="reference internal" href="#publish-subscribe-pattern">Publish/Subscribe pattern</a><ul>
<li><a class="reference internal" href="#typical-pattern">Typical pattern</a></li>
<li><a class="reference internal" href="#implementation-details">Implementation details</a></li>
<li><a class="reference internal" href="#engine-as-a-pubsub-bus">Engine as a pubsub bus</a></li>
<li><a class="reference internal" href="#built-in-channels">Built-in channels</a></li>
<li><a class="reference internal" href="#bus-api">Bus API</a></li>
</ul>
</li>
<li><a class="reference internal" href="#plugins">Plugins</a><ul>
<li><a class="reference internal" href="#create-a-plugin">Create a plugin</a></li>
<li><a class="reference internal" href="#enable-a-plugin">Enable a plugin</a></li>
<li><a class="reference internal" href="#disable-a-plugin">Disable a plugin</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#per-request-functions">Per-request functions</a><ul>
<li><a class="reference internal" href="#hook-point">Hook point</a></li>
<li><a class="reference internal" href="#tools">Tools</a><ul>
<li><a class="reference internal" href="#stateful-tools">Stateful tools</a></li>
<li><a class="reference internal" href="#tools-ordering">Tools ordering</a></li>
<li><a class="reference internal" href="#toolboxes">Toolboxes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#request-parameters-manipulation">Request parameters manipulation</a></li>
</ul>
</li>
<li><a class="reference internal" href="#tailored-dispatchers">Tailored dispatchers</a><ul>
<li><a class="reference internal" href="#tool-or-dispatcher">Tool or dispatcher?</a></li>
</ul>
</li>
<li><a class="reference internal" href="#request-body-processors">Request body processors</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="config.html"
title="previous chapter">Configure</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="deploy.html"
title="next chapter">Deploy</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/extend.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="deploy.html" title="Deploy"
>next</a> |</li>
<li class="right" >
<a href="config.html" title="Configure"
>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>
|