CherryPy

Although the built-in XML-RPC server is really simple to use, I needed a solution that was more scalable in case I had to support widely distributed systems with hundreds of monitoring agents submitting their results. The SimpleXMLRPCServer library by default starts only a single process and therefore is not multithreaded, which means only one connection can be established to it and all other clients will have to wait. This is where CherryPycomes in.

In a nutshell CherryPy is a web application framework that allows rapid development and deployment of web applications. It is written in Python, and not surprisingly the web development language that it supports is also Python.

In addition to being the web application framework, CherryPy is also a web server that complies with RFC2616, which defines the HTTP 1.1 protocol. CherryPy can be used as highly configurable and customizable web server on its own, or in combination with any web server that supports the WSGI interface.

The reason I chose CherryPy to use as an HTTP server and a simple framework for exposing my XML-RPC functions is that it supports multiple socket connections and multithreading out of the box, so I don't have to write any additional code. In addition to that, the framework provides an easy way of configuring it.

It is very simple to use CherryPy. Here is a simple example of a web application that just prints out a static message:

import cherrypy from datetime import datetime class CurrentTime(object): def index(self): return str(datetime.now()) index.exposed = True cherrypy.quickstart(CurrentTime())

This is all you need for a web service that displays the current time. Things are not very different if you want to serve XML-RPC procedures. You just have to inherit your main class from the _cptools.XMLRPCController class and use the @cherrypy.expose decorator function, which effectively registers each function with the framework and also makes it available as a remote procedure. Let's rewrite the hello RPC service using CherryPy:

import cherrypy from cherrypy import _cptools class Root(_cptools.XMLRPCController):

@cherrypy.expose def hello(self, name):

return "Hello, %s" % name cherrypy.quickstart(Root(), '/')

As you can see, the framework adds a very little overhead to the default built-in implementation, but in exchange it provides a multithreaded fully configurable web server and the ability to use it behind enterprise-grade web servers such as Apache.

You can install the latest CherryPy package using Python installer pip with the following command: pip install cherrypy

Was this article helpful?

0 0

Post a comment