PageRenderTime 50ms CodeModel.GetById 42ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/examples/web/filtering.py

https://bitbucket.org/prologic/circuits/
Python | 48 lines | 34 code | 1 blank | 13 comment | 0 complexity | 2edee001b103aa0322284f697ddf98e7 MD5 | raw file
 1#!/usr/bin/env python
 2
 3"""Filtering
 4
 5A simple example showing how to intercept and potentially filter requests.
 6This example demonstrates how you could intercept the response before it goes
 7out changing the response's content into ALL UPPER CASE!
 8"""
 9
10from circuits import handler, Component
11from circuits.web import Server, Controller
12
13
14class Upper(Component):
15
16    channel = "web"  # By default all web related events
17                     # go to the "web" channel.
18
19    @handler("response", priority=1.0)
20    def _on_response(self, response):
21        """Response Handler
22
23        Here we set the priority slightly higher than the default response
24        handler in circutis.web (0.0) so that can we do something about the
25        response before it finally goes out to the client.
26
27        Here's we're simply modifying the response body by changing the content
28        into ALL UPPERCASE!
29        """
30
31        response.body = "".join(response.body).upper()
32
33
34class Root(Controller):
35
36    def index(self):
37        """Request Handler
38
39        Our normal request handler simply returning
40        "Hello World!" as the response.
41        """
42
43        return "Hello World!"
44
45app = Server(("0.0.0.0", 8000))
46Upper().register(app)
47Root().register(app)
48app.run()