Introduction to CherryPy
Since my first article about an Introduction to python web programming I went exploring frameworks to make it even simpler. I wanted something simple and lightweight and preferably supplied by distributions. There are quite some choices and for this article I explored CherryPy. This led me to some reflection into why even use a framework.... and so the article became mostly an outro to CherryPy...
I assume you are familiar with Python and the WSGI approach from my previous article. CherryPy is just a WSGI framework for python. In essence it does exactly what was explained in the previous article but it just does it with more developer friendly options.
Installingen and running CherryPy
On my Ubuntu workstation I can just run:
sudo apt-get install cherrypy3
This works for my Debian server as well.
If you look at the CherryPy website you'll notice it's very easy to write the Hello World app:
import cherrypy
class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True
cherrypy.quickstart(HelloWorld())
The Hello World app runs just fine. The app listens on http://localhost:8080. Couldn't be easier.
Example output:
$ python -u "test.py"
[26/Mar/2012:20:04:11] ENGINE Listening for SIGHUP.
[26/Mar/2012:20:04:11] ENGINE Listening for SIGTERM.
[26/Mar/2012:20:04:11] ENGINE Listening for SIGUSR1.
[26/Mar/2012:20:04:11] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.
[26/Mar/2012:20:04:11] ENGINE Started monitor thread '_TimeoutMonitor'.
[26/Mar/2012:20:04:11] ENGINE Started monitor thread 'Autoreloader'.
[26/Mar/2012:20:04:11] ENGINE Serving on 127.0.0.1:8080
[26/Mar/2012:20:04:11] ENGINE Bus STARTED
127.0.0.1 - - [26/Mar/2012:20:04:42] "GET / HTTP/1.1" 200 12 "" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:11.0) Gecko/20100101 Firefox/11.0"
127.0.0.1 - - [26/Mar/2012:20:04:42] "GET /favicon.ico HTTP/1.1" 200 1406 "" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:11.0) Gecko/20100101 Firefox/11.0"
127.0.0.1 - - [26/Mar/2012:20:04:42] "GET /favicon.ico HTTP/1.1" 200 1406 ""
I wanted to explore the following subjects with CherryPy: 1. JSON output 2. Templating 3. Running in Apache
But reading on I started to dislike CherryPy. It has a lot of beauties but once I started to implement my own classes into CherryPy I discovered that I needed to change those classes to incorporate the exposed = True var. The other way would be to write wrapper classes but I didn't want a framework to get me do more work...
Then there's the full blown CherryPy webserver. Sure it has it's potential but I'm doing fine with the wsgiref one. In production I will use Apache and friends. I started looking for other frameworks and did some tests with Werkzeug. Werkzeug looked promising but I didn't quite like it's routing and dispatching or I didn't want to read more.
Then there was also the issue that my fellow programmers need to get into Python and these frameworks have a learning curve of their own. I don't want them to go through that.
Thinking this through made me wonder why I even needed a framework.
Reading on and on I discovered a lot of those frameworks are based upon WebOB and reading those documents I settled on WebOb. WebOb just makes easy to handle request and response objects of the WSGI interface. This way you don't have to parse the environment of WSGI yourself.
So in the end this blog post led to very useful research but a rather useless article. :-?