| 1 | from zope.interface import implements |
|---|
| 2 | from twisted.internet import reactor |
|---|
| 3 | from twisted.web.resource import IResource, Resource |
|---|
| 4 | from twisted.web import server, guard |
|---|
| 5 | from twisted.cred.portal import IRealm |
|---|
| 6 | from twisted.python import log |
|---|
| 7 | |
|---|
| 8 | import sys |
|---|
| 9 | |
|---|
| 10 | from zope.interface import implements |
|---|
| 11 | |
|---|
| 12 | from twisted.python import log |
|---|
| 13 | from twisted.internet import reactor |
|---|
| 14 | from twisted.web import server, resource, guard |
|---|
| 15 | from twisted.cred.portal import IRealm, Portal |
|---|
| 16 | from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | class Die(Resource): |
|---|
| 21 | isLeaf = True |
|---|
| 22 | def render_GET(self, request): |
|---|
| 23 | reactor.stop() |
|---|
| 24 | return "" # avoid unhandled error |
|---|
| 25 | |
|---|
| 26 | class SimpleRealm(object): |
|---|
| 27 | implements(IRealm) |
|---|
| 28 | |
|---|
| 29 | def requestAvatar(self, avatarId, mind, *interfaces): |
|---|
| 30 | if resource.IResource in interfaces: |
|---|
| 31 | |
|---|
| 32 | root = Resource() |
|---|
| 33 | root.isLeaf = False |
|---|
| 34 | |
|---|
| 35 | foo = Resource() |
|---|
| 36 | foo.isLeaf = False |
|---|
| 37 | |
|---|
| 38 | die = Die() |
|---|
| 39 | foo.putChild("die", die) |
|---|
| 40 | |
|---|
| 41 | root.putChild("foo", foo) |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | return resource.IResource, root, lambda: None |
|---|
| 45 | raise NotImplementedError() |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | if __name__ == "__main__": |
|---|
| 49 | log.startLogging(sys.stdout) |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | # set up /foo/die |
|---|
| 53 | checker = [InMemoryUsernamePasswordDatabaseDontUse(foo='bar')] |
|---|
| 54 | wrapper = guard.HTTPAuthSessionWrapper(Portal(SimpleRealm(), checker), |
|---|
| 55 | [guard.BasicCredentialFactory("/foo/die")]) |
|---|
| 56 | |
|---|
| 57 | reactor.listenTCP(9999, server.Site(wrapper)) |
|---|
| 58 | reactor.run() |
|---|