# CherryPy tutorial

### Remi Delon

#### remi@cherrypy.org

#### http://www.cherrypy.org

# Introduction

- Pythonic, object-oriented web development framework
- Runs everywhere python runs
- Requires Python-2.3 or later
- BSD license
- Version 2.1 (used in this tutorial) coming soon

# Brief history of CherryPy

- Jun 2002: CherryPy-0.1 released: users wrote .cpy files made of python code + some extensions (ex: CherryClass), then compiled them into one .py file ... bad idea (too unpythonic) ... some interest, but quite limited
- Apr 2004: Last "CP1" release
- Sep 2004: First "CP2" release: pure python code, no more compilation step, templating system separated from the core ... much more interest from the community
- In a few weeks: CherryPy-2.1 release ... another big step forward

# Hello, world

- Exposing objects
- Built-in HTTP server
- "development" environment: autoreload, debug information

# Hello, you

- Passing parameters to methods
- All parameters (except file uploads) are passed a strings or lists of strings
- GET or POST parameters are passed the same way

# Configuring CherryPy

- Through config file
- Programmatically

# Url -> object mapping

- Using multiple methods
- Using multiple classes
- Using the "default" method
- Using positional arguments for REST URLs

# Using OOP

- Example: using OOP to provide a constant header/footer

# Handling file uploads

- File is stored on disk as a temp file
- CherryPy returns a cgi.FieldStorage instance
- Available attributes: file, filename, type, ...

# Manipulating cpg.request and cpg.response

- Ex: Reading the Host header from the request
- Ex: Setting the response Content-Type

# Using generators

- Returning a generator instead of a string
- Returning a file object
- Set Content-Length manually to stream data to the browser "on-the-fly"

# Using filters

- Ex: gzipFilter (uses generators)
- Ex: encodingFilter, decodingFilter
- Ex: staticContentFilter
- Other filters: XML-RPC, Tidy, nsgmls, virtual host, caching, ...

# Using sessions

- Sessions are just a regular filter (sessionFilter)
- Provides dictionary-like objects for storing session data
- The default one is cherrypy.sessions.default

# Deploying your site for production

- Built-in HTTP server: OK, but no HTTP/1.1 or SSL (yet)
- WSGI interface allows us to use a variety of setups: mod_python, FastCGI, IIS, ...
- Another option: behind Apache with mod_rewrite or mod_proxy (use Apache to serve static data directly)

# What's not included in CherryPy

- A form handling module
- A templating system
- An authentication system
- ... CherryPy is deliberately staying low-level
- ... If you need a higher-level framework, consider Subway or CherryObject (coming soon)

# References

- http://www.cherrypy.org
- Documentation: tutorial, FAQ, recipes, ...
- Mailing lists
- IRC channel: #cherrypy on OFTC

# Near future of CherryPy

- CherryPy-2.1 coming out soon (*lots* of new features)
- CherryPy book (online only for now) coming soon
- Lots of activity at the moment ... More and more contributors
- But we could still use more: feel free to help :-)

# Any questions ?