CherryPy Essentials

Rapid Python Web Application Development Design, develop, test, and deploy your Python web applications easily Rapid Python Web Application Development Design, develop, test, and deploy your Python web applications easily Rapid Python Web Application Development All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations...

Extending the Data Access Layer

In the previous sections, we have defined the mapping between our entities and the classes our application will manipulate. As they stand these classes are not very useful in this section we will see how to extend them to provide more functionalities. To keep this section concise, we will only discuss the Album class. Methods to be added to the Album class Returns all the attached films return self.Film() films property(films) Returns all the existing albums sandbox arena.new_sandbox() return...

Installation from a Tarball

A tarball is a compressed archive of a file or directory. The name comes from the use of the tar utility found on UNIX and related operating systems. Historically the compression used has usually been gzip I NiS and the extension of a tarball is either .tar.gz or .tgz. I CherryPy provides a tarball for each release whether it is alpha, beta, release candidate, or stable. They can all be retrieved from http download. cherrypy. org . CherryPy tarballs contain the complete source code of the...

Installation from Subversion

Subversion is often viewed as the successor of CVS and is considered much friendlier. However, other revision systems also exist such as Monotone or Darcs. Under Linux, you can either install Subversion from its sources or using a package manager. Let's describe the procedure for the source code. 1. Get the latest tarball from http subversion.tigris.org 2. Then type the following command in the command console tar zxvf subversion-x.y.z.tar.gz 3. Enter the newly created directory and type ....

Installation through Easy Install

Easy_install is a Python module that can be found on the Python Enterprise Application Kit (PEAK) website to facilitate the deployment of Python packages and modules. From the developer's point of view, it provides an easy API to import Python modules either for a given version or a range of versions. For instance, here is what you would do to load the first CherryPy version greater than 2.2 found in your environment > > > from pkg_resources import require > > > require(cherrypy>...

Uniform Resource Identifier

REST is about naming resources on a network and providing a unified mechanism to perform operations on these resources. That's why REST tells us that a resource is identified by at least one identifier. When implementing a REST infrastructure based on the HTTP protocol, these identifiers are defined as Uniform Resource Identifiers (URIs). Two common subsets of the URI set are Uniform Resource Locator (URL), such as Uniform Resource Name (URN), such as The interesting aspect of URLs is that they...

Unit Testing Web Applications

In the previous section, we have presented two standard modules to perform unit testing in Python applications and packages. Unfortunately as they stand they lack some common features to help in specific contexts such as web applications. The Python community has obviously come up with solutions and there are several good extensions to unittest or completely distinct test packages to help us. We will use an extension to unittest, provided by CherryPy, called webtest and developed by Robert...

Error and Exception Handling

CherryPy tries hard to help the developer see a web application as close as a rich application. This means that from your page handler you may raise a Python error or exception as in any other Python application. CherryPy will catch those and transform them into HTTP messages depending on the type of errors. Note that when an exception is raised and not caught by any other part of the application, CherryPy will return the corresponding HTTP 500 error code. For example, the following example...

Hook into Cherry Pys Core Engine

Call Handling Process

One of the most powerful aspects of CherryPy is how its core lets you modify its normal behavior with a very fine granularity. Indeed, CherryPy offers a mechanism called hooking to customize the core engine. A hook is an entry point for Python callables to be applied at specific points during the request processing. CherryPy provides the following entry points Called at the beginning of the process. Called before CherryPy tries to read the request body. It allows a tool to inform CherryPy...

Application under Test

Before we explain how Selenium components work, we must introduce an application example. This application will simply provide one web page with two links. One of them will replace the current page with a new one. The second link will fetch data using Ajax. We use this example rather than our photoblog application for the sake of simplicity. The code of the application is as follows _header lt html gt lt head gt lt title gt Selenium test lt title gt lt head gt lt script type application...

Cherrypy Json Javascript

As we have already seen in this chapter, in spite of carrying XML in its name, Ajax does not prevent other formats being carried. For instance, one extremely common format that you will see is JSON JavaScript Object Notation . In a nutshell, JSON is a way to carry serialized JavaScript objects so that a JavaScript application can evaluate them and transform them into JavaScript objects that the application can manipulate. For instance, when the user requests the server for an album object...

Authentication using Digest or Basic Schemes

The open method of XMLHttpRequest can take username and password parameters to be sent along with the request. The authentication schemes supported by XMLHttpRequest are defined in RFC 2617, namely basic and digest. These two schemes are as follows Basic scheme The basic scheme is simply the transfer of the username and password encoded using the base64 algorithm. The issue with this is that, if a third-party fetches the encoded value, nothing can be done to prevent it from being decoded. This...