Getting started with CherryPy

I mentioned briefly last week that I was getting started with CherryPy and how impressed I was with the ease of the install. I also finally figured out that Ubuntu has a psycopg package (python-psycopg), although I still couldn't get web.py to work with it.

Here's a list of the packages that I had to install on my Ubuntu 9.0.4 server to get CherryPy running

>

apt-get install python

apt-get install python-setuptools

apt-get install flup

apt-get install postgres

apt-get install postgresql postgresql-client postgresql-contrib

apt-get install pgdb

apt-get install postgresql-python

apt-get install python-pygresql

apt-get install gcc

apt-get install g++

apt-get install postgresql-dev

apt-get install libpq-dev

apt-get install python-psycopg

Want to get a "Hello World" example of CherryPy working? It's incredible easy, especially if you use CherryPy's built-in webserver (which seems like it'll handle the sort of loads I'm going to be dealing with).

Here's the code:

~~~~ {.python name="code"} import cherrypycherrypy.config.update( {'server.socket_host': '127.0.0.1', 'server.socket_port': 80, })class HelloWorld: def index(self): return "Hello World" index.exposed = Truecherrypy.quickstart(HelloWorld()) wzxhzdk:0

~~~~ {.python name="code"} import cherrypyimport psycopgimport syscherrypy.config.update( {'server.socket_host': '10.0.0.4', 'server.socket_port': 80, })class HelloWorld: def index(self): conn = psycopg.connect('dbname=testdb', 'user=myuser', 'password=mypass') mark = conn.cursor() mark.execute('SELECT * FROM todo'); rec = mark.fetchall() body = '' for row in rec: body += str(row) return body index.exposed = Truecherrypy.quickstart(HelloWorld()) ~~~~

That's a very basic intro to CherryPy. The complete documentation has lots of great stuff and I'll be back to write more about it after I've had a chance to write something real.