Welcome to the Colubrid quickstart. This tutorial explains the very basics of Colubrid.
For our first application we will use the BaseApplication dispatcher:
from colubrid import BaseApplication, HttpResponse, execute
class Application(BaseApplication):
def process_request(self):
return HttpResponse('Hello World')
app = Application
if __name__ == '__main__':
execute(app)
The result is a full WSGI compatible application. When called from within a WSGI wrapper, you should see the output "Hello World".
You can try out the application using the paste server by executing your application from the command line:
python myapplication.py
So. What this all about? A plain WSGI application is something like a big Python iterator. Because this is stupid to work with, Colubrid encapsulates the whole application within the BaseApplication object and provides a request object and process_request method.
There are many other application types explained later. But the thing common to each application is: They all override the process_request method of BaseApplication.
When you send output to the browser via request.write, all sent data is buffered inside the request object and only sent to the client when the application calls the iterator of the request.
This allows you to modify the response headers after sending output.
Colubrid features a number of application types that implement URL dispatchers.
This allows you to bind method to URLs. Demonstrated here is the RegexApplication, an application type which maps methods to regular expressions:
from colubrid import RegexApplication, HttpResponse, execute
class MyApplication(RegexApplication):
urls = [
(r'^$', 'index'),
(r'^downloads$', 'downloads')
]
def index(self):
return HttpResponse('Index')
def downloads(self):
return HttpResponse('Downloads')
app = MyApplication
if __name__ == '__main__':
execute()