File: filtering.py

package info (click to toggle)
circuits 3.1.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 9,764 kB
  • sloc: python: 15,945; makefile: 131
file content (48 lines) | stat: -rwxr-xr-x 1,259 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python

"""Filtering

A simple example showing how to intercept and potentially filter requests.
This example demonstrates how you could intercept the response before it goes
out changing the response's content into ALL UPPER CASE!
"""

from circuits import handler, Component
from circuits.web import Server, Controller


class Upper(Component):

    channel = "web"  # By default all web related events
                     # go to the "web" channel.

    @handler("response", priority=1.0)
    def _on_response(self, response):
        """Response Handler

        Here we set the priority slightly higher than the default response
        handler in circutis.web (0.0) so that can we do something about the
        response before it finally goes out to the client.

        Here's we're simply modifying the response body by changing the content
        into ALL UPPERCASE!
        """

        response.body = "".join(response.body).upper()


class Root(Controller):

    def index(self):
        """Request Handler

        Our normal request handler simply returning
        "Hello World!" as the response.
        """

        return "Hello World!"

app = Server(("0.0.0.0", 8000))
Upper().register(app)
Root().register(app)
app.run()