Servable

Servable is a nifty little webservice framework. Just subclass Servable to turn an object into a webservice.

Get Servable

$ svn checkout http://servable.googlecode.com/svn/trunk/ servable
$ cd servable
$ sudo python setup.py install

How it Works

Create a class that subclasses Servable. Instantate that class, and then call wsgi_app() to get a wsgi app object which transforms that object instance into a webservice. For example, take this short Pythons script defining a class A:
from servable import Servable

class A(Servable):
    def echo(self, phrase="Hello, World!"):
      return phrase
    
a = A()
wsgi_app = a.wsgi_app()
The varibale 'wsgi_app' contains a method object that webservers can use as a gateway to your class, though explaining how is beyond our scope here. If you're in a hurry to see some results, you can do this:
a.run_test_server()
http://localhost:8080/echo?phrase="foobar" returns "foobar"
http://localhost:8080/echo returns "Hello, world!"

For Example

The script that runs this site (via Google App Engine) looks like this:
import wsgiref.handlers
from servable import Servable

class Site(Servable):
    def __init__(self):
        self.answers = {}
    
    def index(self):
        """The webpage"""
        return open("index.html").read()
    index.path = "/$"
    index.mime = "text/html"
    
    def add(self, a, b=100):
        """Add two integers"""
        return a+b
    
    def yell(self, phrase):
        """Uppercase everything"""
        return phrase.upper()
    
    def fib(self, n):
        """Return nth fibonacci number"""
        
        if n<0 or n>1958:
            raise Exception("n must be between 0 and 1958")
        
        if n in self.answers:
            return self.answers[n]
        
        if n==0:
            self.answers[n] = 0
        elif n==1:
            self.answers[n] = 1
        else:
            self.answers[n] = self.fib(n-2)+self.fib(n-1)
            
        return self.answers[n]
        
if __name__ == "__main__":
  site = Site()
  wsgiref.handlers.CGIHandler().run( site.wsgi_app() )
Give it a spin:

About

Servable was factored out of Graphserver by Brandon Martin-Anderson. Big ups to UrbanMapping for countenancing this work.