Book Image

CherryPy Essentials: Rapid Python Web Application Development

By : Sylvain Hellegouarch
Book Image

CherryPy Essentials: Rapid Python Web Application Development

By: Sylvain Hellegouarch

Overview of this book

<p>CherryPy is a Python library for web development that allows developers to build web applications in the same way as any other object-oriented Python program. Enriched by several years of active development, it has become one of the most established toolkits for building solid and high-performance web applications in Python. CherryPy abstracts the complex low-level HTTP protocol into an easy-to-use interface that respects Python idioms. The library aims at being simple to learn for a beginner while offering the most advanced features to fluent Python developers. For these reasons CherryPy was chosen to be at the heart of the popular and feature-rich TurboGears web framework. CherryPy-powered web applications are stand-alone Python applications with their own embedded multi-threaded web server, but can also run behind Apache or IIS for scalability.</p>
Table of Contents (17 chapters)
CherryPy Essentials
Credits
About the Author
Acknowledgement
About the Reviewers
Preface
Index

Static Resource Serving


CherryPy provides two simple tools to serve either a single file or an entire directory. In either case CherryPy takes care of the HTTP caching aspect of your static resource by automatically checking the presence of the If-Modified-Since and If-Unmodified-Since headers in the request and returning directly the 304 Not Modified response, if that's the case.

Using the Staticfile Tool to Serve a Single File

The staticfile tool can be used to serve a single file.

Arguments:

Name

Default

Description

filename

N/A

Absolute or relative path to the physical file.

root

None

If filename is relative you must provide the root directory of the file.

match

""

Regular expression to check that the URI path matches a certain pattern.

content_types

None

Dictionary of the form ext: mime type.

Example:

For this purpose let's imagine we have the following directory layout:

application \
        myapp.py
        design1.css

design1.css is set as follows:

body {
    background-color...