PageRenderTime 17ms CodeModel.GetById 7ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/web/authdemo.py

https://bitbucket.org/prologic/circuits/
Python | 39 lines | 19 code | 13 blank | 7 comment | 2 complexity | 0e3230e378fa0b7adfc8209d4c823014 MD5 | raw file
 1#!/usr/bin/env python
 2
 3
 4from hashlib import md5
 5
 6
 7from circuits import handler, Component
 8from circuits.web import Server, Controller
 9from circuits.web.tools import check_auth, basic_auth
10
11
12class Auth(Component):
13
14    realm = "Test"
15    users = {"admin": md5("admin").hexdigest()}
16
17    @handler("request", priority=1.0)
18    def on_request(self, event, request, response):
19        """Filter Requests applying Basic Authentication
20
21        Filter any incoming requests at a higher priority than the
22        default dispatcher and apply Basic Authentication returning
23        a 403 Forbidden response if Authentication failed.
24        """
25
26        if not check_auth(request, response, self.realm, self.users):
27            event.stop()
28            return basic_auth(request, response, self.realm, self.users)
29
30
31class Root(Controller):
32
33    def index(self):
34        return "Hello World!"
35
36app = Server(("0.0.0.0", 8000))
37Auth().register(app)
38Root().register(app)
39app.run()