summaryrefslogtreecommitdiff
path: root/sphinx/source/basics.rst
blob: 60e7368a02706a5380b4d095ebdde9f722922930 (plain)
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

Basics
------

The following sections will drive you through the basics of
a CherryPy application, introducing some essential concepts.

The one-minute application example
##################################

The most basic application you can write with CherryPy 
involves almost all its core concepts.

.. code-block:: python
   :linenos:

   import cherrypy
   
   class Root(object):
       @cherrypy.expose
       def index(self):
           return "Hello World!"

   if __name__ == '__main__':
      cherrypy.quickstart(Root(), '/')


First and foremost, for most tasks, you will never need more than
a single import statement as demonstrated in line 1.

Before discussing the meat, let's jump to line 9 which shows,
how to host your application with the CherryPy application server
and serve it with its builtin HTTP server at the `'/'` path. 
All in one single line. Not bad.

Let's now step back to the actual application. Even though CherryPy
does not mandate it, most of the time your applications 
will be written as Python classes. Methods of those classes will
be called by CherryPy to respond to client requests. However,
CherryPy needs to be aware that a method can be used that way, we
say the method needs to be :term:`exposed`. This is precisely
what the :func:`cherrypy.expose()` decorator does in line 4. 

Save the snippet in a file named `myapp.py` and run your first
CherryPy application:

.. code-block:: bash

   $ python myapp.py

Then point your browser at http://127.0.0.1:8080. Tada!


.. note::

   CherryPy is a small framework that focuses on one single task: 
   take a HTTP request and locate the most appropriate
   Python function or method that match the request's URL. 
   Unlike other well-known frameworks, CherryPy does not 
   provide a built-in support for database access, HTML
   templating or any other middleware nifty features. 

   In a nutshell, once CherryPy has found and called an 
   :term:`exposed` method, it is up to you, as a developer, to
   provide the tools to implement your application's logic.

   CherryPy takes the opinion that you, the developer, know best.

.. warning::

   The previous example demonstrated the simplicty of the
   CherryPy interface but, your application will likely
   contain a few other bits and pieces: static service,
   more complex structure, database access, etc. 
   This will be developed in the tutorial section.


CherryPy is a minimal framework but not a bare one, it comes
with a few basic tools to cover common usages that you would
expect.

Hosting one or more applications
################################

A web application needs an HTTP server to be accessed to. CherryPy
provides its own, production ready, HTTP server. There are two
ways to host an application with it. The simple one and the almost-as-simple one.

Single application
^^^^^^^^^^^^^^^^^^

The most straightforward way is to use :func:`cherrypy.quickstart`
function. It takes at least one argument, the instance of the 
application to host. Two other settings are optionals. First, the
base path at which the application will be accessible from. Second,
a config dictionary or file to configure your application.

.. code-block:: python

   cherrypy.quickstart(Blog())
   cherrypy.quickstart(Blog(), '/blog')
   cherrypy.quickstart(Blog(), '/blog', {'/': {'tools.gzip.on': True}})

The first one means that your application will be available at
http://hostname:port/ whereas the other two will make your blog
application available at http://hostname:port/blog. In addition,
the last one provides specific settings for the application.

.. note::

   Notice in the third case how the settings are still 
   relative to the application, not where it is made available at, 
   hence the `{'/': ... }` rather than a `{'/blog': ... }`


Multiple applications
^^^^^^^^^^^^^^^^^^^^^

The :func:`cherrypy.quickstart` approach is fine for a single application,
but lacks the capacity to host several applications with the server.
To achieve this, one must use the :func:`cherrypy.tree.mount` function as 
follow:

.. code-block:: python

   cherrypy.tree.mount(Blog(), '/blog', blog_conf)
   cherrypy.tree.mount(Forum(), '/forum', forum_conf)
   
   cherrypy.engine.start()
   cherrypy.engine.block()

Essentially, :func:`cherrypy.tree.mount` takes the same parameters
as :func:`cherrypy.quickstart`: an application, a hosting path segment
and a configuration. The last two lines are simply starting
application server.

.. note::

   :func:`cherrypy.quickstart` and :func:`cherrypy.tree.mount` are not
   exclusive. For instance, the previous lines can be written as:

   .. code-block:: python

      cherrypy.tree.mount(Blog(), '/blog', blog_conf)
      cherrypy.quickstart(Forum(), '/forum', forum_conf)


Logging
#######

Logging is an important task in any application. CherryPy will
log all incoming requests as well as protocol errors.

To do so, CherryPy manages two loggers:

- an access one that logs every incoming requests 
- an application/error log that traces errors or other application-level messages

Your application may leverage that second logger by calling
:func:`cherrypy.log()`. 

.. code-block:: python

   cherrypy.log("hello there")

You can also log an exception:

.. code-block:: python

   try:
      ...
   except:
      cherrypy.log("kaboom!", traceback=True)

Both logs are writing to files identified by the following keys
in your configuration:

- `log.access_file` for incoming requests using the 
  `common log format <http://en.wikipedia.org/wiki/Common_Log_Format>`_
- `log.error_file` for the other log

Disable logging
^^^^^^^^^^^^^^^

You may be interested in disabling either logs. To do so, simply
set a en empty string to the `log.access_file` or `log.error_file`
parameters.

Play along with your other loggers
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Your application may aobviously already use the :mod:`logging`
module to trace application level messages. CherryPy will not
interfere with them as long as your loggers are explicitely
named. This would work nicely:

.. code-block:: python
		
    import logging
    logger = logging.getLogger('myapp.mypackage')
    logger.setLevel(logging.INFO)
    stream = logging.StreamHandler()
    stream.setLevel(logging.INFO)
    logger.addHandler(stream)

.. _config:

Configuring
###########

CherryPy comes with a fine-grained configuration mechanism and 
settings can be set at various levels.

Global server settings
^^^^^^^^^^^^^^^^^^^^^^

To configure the HTTP and application servers, 
use the :meth:`cherrypy.config.update() <cherrypy._cpconfig.Config.update>` 
method.

.. code-block:: python

   cherrypy.config.update({'server.socket_port': 9090})

The :mod:`cherrypy.config <cherrypy._cpconfig>` object is a dictionary and the 
update method merge the passed dictionary into it.

You can also pass a file instead (assuming a `server.conf`
file):

.. code-block:: ini

   [global]
   server.socket_port: 9090

.. code-block:: python

   cherrypy.config.update("server.conf")

.. warning::

   :meth:`cherrypy.config.update() <cherrypy._cpconfig.Config.update>`  
   is not mean to be used to configure the application. 
   It is a common mistake. It is used to configure the server and engine.

.. _perappconf:

Global application settings
^^^^^^^^^^^^^^^^^^^^^^^^^^^

To configure your application settings, pass a dictionary
or a file when you associate ther application
to the server.

.. code-block:: python

   cherrypy.quickstart(myapp, '/', {'/': {'tools.gzip.on': True}})

or via a file (called `app.conf` for instance):

.. code-block:: ini

   [/]
   tools.gzip.on: True

.. code-block:: python

   cherrypy.quickstart(myapp, '/', "app.conf")
 

Local application settings
^^^^^^^^^^^^^^^^^^^^^^^^^^

Although, you can define most of your settings in a global
fashion, it is sometimes convenient to define them
where they are applied in the code.

.. code-block:: python

   class Root(object):
       @cherrypy.expose
       @cherrypy.tools.gzip()
       def index(self):
           return "hello world!"

A variant notation to the above:

.. code-block:: python

   class Root(object):
       @cherrypy.expose
       def index(self):
           return "hello world!"
       index._cp_config = {'tools.gzip.on': True}

Both methods have the same effect so pick the one
that suits your style best.

.. _basicsession:

Using sessions
##############

Sessions is one of the most common mechanism used by developers to 
identify users and synchronize their activity. By default, CherryPy
does not activate sessions because it is not a mandatory feature
to have, to enable it simply add the following settings in your
configuration:

.. code-block:: ini

   [/]
   tools.sessions.on: True

.. code-block:: python

   cherrypy.quickstart(myapp, '/', "app.conf")
 
Sessions are, by default, stored in RAM so, if you restart your server
all of your current sessions will be lost. You can store them in memcached
or on the filesystem instead.

Using sessions in your applications is done as follow:

.. code-block:: python

   import cherrypy
  
   @cherrypy.expose
   def index(self):
       if 'count' not in cherrypy.session:
          cherrypy.session['count'] = 0
       cherrypy.session['count'] += 1

In this snippet, everytime the the index page handler is called,
the current user's session has its `'count'` key incremented by `1`.

CherryPy knows which session to use by inspecting the cookie
sent alongside the request. This cookie contains the session
identifier used by CherryPy to load the user's session from
the storage.

Filesystem backend
^^^^^^^^^^^^^^^^^^

Using a filesystem is a simple not to lose your sessions
between reboots. Each session is saved in its own file within
the given directory. 

.. code-block:: ini

   [/]
   tools.sessions.on: True
   tools.sessions.storage_type = "file"
   tools.sessions.storage_path = "/some/directorys"

Memcached backend
^^^^^^^^^^^^^^^^^

`Memcached <http://memcached.org/>`_ is a popular key-store on top of your RAM, 
it is distributed and a good choice if you want to
share sessions outside of the process running CherryPy.

.. code-block:: ini

   [/]
   tools.sessions.on: True
   tools.sessions.storage_type = "memcached"

.. _staticontent:

Static content serving
######################

CherryPy can serve your static content such as images, javascript and 
CSS resources, etc. 

Serving a single file
^^^^^^^^^^^^^^^^^^^^^

You can serve a single file as follow:

.. code-block:: ini

   [/style.css]
   tools.staticfile.on = True
   tools.staticfile.filename = "/home/site/style.css"

CherryPy will automatically respond to URLs such as 
`http://hostname/style.css`.

Serving a whole directory
^^^^^^^^^^^^^^^^^^^^^^^^^

Serving a whole directory is similar to a single file:

.. code-block:: ini

   [/static]
   tools.staticdir.on = True
   tools.staticdir.dir = "/home/site/static"

Assuming you have a file at `static/js/my.js`, 
CherryPy will automatically respond to URLs such as 
`http://hostname/static/js/my.js`.


.. note::

   CherryPy always requires the absolute path to the files or directories
   it will serve. If you have several static section to configure
   but located in the same root directory, you can use the following 
   shortcut:

   
   .. code-block:: ini

      [/]
      tools.staticdir.root = "/home/site"

      [/static]
      tools.staticdir.on = True
      tools.staticdir.dir = "static"

Dealing with JSON
#################

CherryPy has a built-in support for JSON encoding and decoding
of the request and/or response.

Decoding request
^^^^^^^^^^^^^^^^

To automatically decode the content of a request using JSON:

.. code-block:: python

   class Root(object):
       @cherrypy.expose
       @cherrypy.tools.json_in()
       def index(self):
           data = cherrypy.request.json

The `json` attribute attached to the request contains
the decoded content.

Encoding response
^^^^^^^^^^^^^^^^^

To automatically encode the content of a response using JSON:

.. code-block:: python

   class Root(object):
       @cherrypy.expose
       @cherrypy.tools.json_out()
       def index(self):
           return {'key': 'value'}

CherryPy will encode any content returned by your page handler
using JSON. Not all type of objects may natively be
encoded.

Authentication
##############

CherryPy provides support for two very simple authentications mechanism,
both described in :rfc:`2617`: Basic and Digest. They are most commonly
known to trigger a browser's popup asking users their name
and password.

Basic
^^^^^

Basic authentication is the simplest form of authentication however
it is not a secure one as the user's credentials are embedded into
the request. We advise against using it unless you are running on
SSL or within a closed network.

.. code-block:: python

   from cherrypy.lib import auth_basic

   USERS = {'jon': 'secret'}

   def validate_password(username, password):
       if username in USERS and USERS[username] == password:
          return True
       return False

   conf = {
      '/protected/area': {
          'tools.auth_basic.on': True,
          'tools.auth_basic.realm': 'localhost',
          'tools.auth_basic.checkpassword': validate_password
       } 
   }

   cherrypy.quickstart(myapp, '/', conf)

Simply put, you have to provide a function that will
be called by CherryPy passing the username and password 
decoded from the request.

The function can read its data from any source it has to: a file,
a database, memory, etc.


Digest
^^^^^^

Digest authentication differs by the fact the credentials
are not carried on by the request so it's a little more secure
than basic.

CherryPy's digest support has a similar interface to the 
basic one explained above.

.. code-block:: python

   from cherrypy.lib import auth_digest

   USERS = {'jon': 'secret'}

   conf = {
      '/protected/area': {
           'tools.auth_digest.on': True,
           'tools.auth_digest.realm': 'localhost',
           'tools.auth_digest.get_ha1': auth_digest.get_ha1_dict_plain(USERS),
           'tools.auth_digest.key': 'a565c27146791cfb'
      }
   }

   cherrypy.quickstart(myapp, '/', conf)

Favicon
#######

CherryPy serves its own sweet red cherrypy as the default 
`favicon <http://en.wikipedia.org/wiki/Favicon>`_ using the static file
tool. You can serve your own favicon as follow:

.. code-block:: python

    import cherrypy

    class HelloWorld(object):
       @cherrypy.expose
       def index(self):
           return "Hello World!"

    if __name__ == '__main__':
        cherrypy.quickstart(HelloWorld(), '/',
            {
                '/favicon.ico':
                {
                    'tools.staticfile.on': True,
                    'tools.staticfile.filename:' '/path/to/myfavicon.ico'
                }
            }
        )

Please refer to the :ref:`static serving <staticontent>` section
for more details.

You can also use a file to configure it:

.. code-block:: ini

    [/favicon.ico]
    tools.staticfile.on: True
    tools.staticfile.filename: "/path/to/myfavicon.ico"


.. code-block:: python

    import cherrypy

    class HelloWorld(object):
       @cherrypy.expose
       def index(self):
           return "Hello World!"

    if __name__ == '__main__':
        cherrypy.quickstart(HelloWorld(), '/', app.conf)