Design, develop, test, and deploy your Python web applications easily
Walks through building a complete Python web application using CherryPy 3
- The CherryPy HTTP:Python interface
- Use CherryPy with other Python libraries
- Design, security, testing, and deployment
In Detail
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.
What you will learn from this book?
- The book will be about CherryPy 3
- CherryPy installation using the tarball, egg, and subversion
- The CherryPy library in depth: its design and how to use it
- Using CherryPy with common Python and non-Python products
- Object Relational Mapping with SQLObject, SQLAlchemy, and Dejavu
- Web Services via REST and the Atom Publishing Protocol
- AJAX and client-side scripting via MochiKit
- Unit, functional, and load testing
- CherryPy, Apache, and lighttpd deployment solutions
Approach
The book starts with two chapters dedicated to CherryPy, its history and installation. It then moves through a gentle introduction to the main features and concepts of CherryPy to a thorough review of its core capabilities. After laying this foundation the book dives into a real-world example, detailing important steps in the creation of a photoblog application. These chapters cover database manipulation via Object Relational Mapping (ORM), using web services to enhance the application, and client-side scripting through AJAX. Finally, the book covers the need for unit, functional, and performance testing before closing with deployment solutions for a CherryPy-based application.
Who this book is written for?
This book is principally geared towards Python web developers, who are looking to add the power of the CherryPy library to their existing Python skillset. It assumes a good working knowledge of Python. Although the CherryPy toolkit is at the core of the book, many other common libraries are introduced
Author(s): Sylvain Hellegouarch
Publisher: Packt Publishing
Year: 2007
Language: English
Pages: 270
CherryPy Essentials......Page 1
Table of Contents......Page 8
Preface......Page 14
Overview......Page 20
History of CherryPy......Page 21
The Community......Page 22
CherryPy Project Strengths......Page 23
Through the Book......Page 24
Summary......Page 25
Requirements......Page 26
Overview......Page 27
Installation from a Tarball......Page 29
Installation through Easy Install......Page 31
Installation from Subversion......Page 33
Keeping CherryPy Up to Date......Page 36
Summary......Page 37
Vocabulary......Page 38
Basic Example......Page 39
Internal Engine......Page 45
Configuration......Page 46
Object Publisher Engine......Page 49
Library......Page 51
The Coverage Module......Page 52
The Profiler Module......Page 53
The Sessions Module......Page 54
The XML-RPC Module......Page 55
Tools......Page 56
Error and Exception Handling......Page 57
Summary......Page 62
HTTP Compliance......Page 64
Multiple HTTP Servers......Page 65
Multi-Threaded Application Server......Page 67
HTTP Method Dispatcher......Page 68
Routes Dispatcher......Page 70
Virtual Host Dispatcher......Page 71
Hook into CherryPy's Core Engine......Page 72
CherryPy Toolbox......Page 74
Basic Authentication Tool......Page 75
Caching Tool......Page 76
Decoding Tool......Page 77
Digest Authentication Tool......Page 78
Encode Tool......Page 79
Etag Tool......Page 80
Ignore Headers Tool......Page 82
Log Headers Tool......Page 83
Log Tracebacks Tool......Page 84
Proxy Tool......Page 85
Referer Tool......Page 86
Response Headers Tool......Page 87
Trailing Slash Tool......Page 88
XML-RPC Tool......Page 89
Creating a Tool......Page 90
Using the Staticfile Tool to Serve a Single File......Page 94
Using the Staticdir Tool to Serve a Complete Directory......Page 96
Bypassing Static Tools to Serve Static Content......Page 98
WSGI Support......Page 99
Hosting a WSGI Application within the CherryPy WSGI Server......Page 100
Hosting a CherryPy WSGI Application within a Third-Party WSGI Server......Page 102
Summary......Page 103
A Photoblog Application......Page 104
Photoblog Entities......Page 105
Vocabulary......Page 107
Relational Database Management System (RDBMS)......Page 108
Object-Oriented Database Management System (OODBMS)......Page 109
Object-Relational Mapping......Page 110
Python Object-Relational Mappers......Page 111
Photoblog Application Entity Modeling......Page 121
Mapping Entities......Page 122
Units and UnitProperties......Page 124
The Sandbox Interface......Page 125
Querying Units......Page 126
Extending the Data Access Layer......Page 127
Summary......Page 130
Traditional Web Development......Page 132
Separation of Concerns......Page 134
REST......Page 135
Uniform Resource Identifier......Page 136
HTTP Methods......Page 137
Putting it Together......Page 141
REST Interface through CherryPy......Page 143
Atom Publishing Protocol......Page 144
Atom XML-Document Format......Page 145
APP Implementation......Page 147
Summary......Page 149
HTML......Page 150
XHTML......Page 151
CSS......Page 152
DHTML......Page 154
Overview......Page 155
Conditional Statement......Page 157
Looping Mechanism......Page 158
Extensibility......Page 159
Targetting the User Agent......Page 160
Global Design Goals......Page 161
CherryPy—Encapsulating the Template Rendering Process......Page 162
Basic Structure......Page 164
Mochikit......Page 169
HTML Code......Page 170
Handling the End-User Actions......Page 171
Amending the CSS......Page 172
Let's be More Flexible.........Page 173
Summary......Page 174
Rise of the Rich-Client Applications......Page 176
Ajax......Page 177
Ajax—Advantages and Drawbacks......Page 178
Behind the Scene: XMLHttpRequest......Page 179
Performing a GET Request......Page 180
Performing a Content-Negotiated GET Request......Page 181
Performing a POST Request......Page 182
Authentication using Digest or Basic Schemes......Page 183
JSON......Page 189
Defining the Required Namespaces......Page 191
Adding Methods to the Classes......Page 192
Method to Create a New Album......Page 196
Method to Delete an Existing Album......Page 203
Summary......Page 204
Why Testing......Page 206
Planning a Test......Page 207
Unit Testing......Page 208
unittest......Page 209
doctest......Page 214
Unit Testing Web Applications......Page 218
Performance and Load Testing......Page 226
Functional Testing......Page 231
Application under Test......Page 232
Selenium Core......Page 235
Selenium IDE......Page 240
Selenium Remote Control......Page 244
Summary......Page 246
CherryPy—Web and Engine Configuration System......Page 248
Photoblog Application Configuration System......Page 251
Deployment......Page 253
Apache with mod_rewrite Module......Page 254
Lighttpd with mod_proxy Module......Page 256
Apache with mod_python Module......Page 257
SSL......Page 259
Creating a Certificate and a Private Key......Page 260
Using the CherryPy SSL Support......Page 261
Using the lighttpd SSL Support......Page 263
Summary......Page 264
Index......Page 266