~canonical-livepatch-dependencies/canonical-livepatch-service-dependencies/twisted

« back to all changes in this revision

Viewing changes to docs/core/examples/pbecho.py

  • Committer: Free Ekanayaka
  • Date: 2016-07-01 12:22:33 UTC
  • Revision ID: free.ekanayaka@canonical.com-20160701122233-nh55w514zwzoz1ip
Tags: upstream-16.2.0
ImportĀ upstreamĀ versionĀ 16.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
if __name__ == '__main__':
 
5
    # Avoid using any names defined in the "__main__" module.
 
6
    from pbecho import main
 
7
    raise SystemExit(main())
 
8
 
 
9
from zope.interface import implements
 
10
 
 
11
from twisted.spread import pb
 
12
from twisted.cred.portal import IRealm
 
13
 
 
14
class DefinedError(pb.Error):
 
15
    pass
 
16
 
 
17
 
 
18
class SimplePerspective(pb.Avatar):
 
19
 
 
20
    def perspective_echo(self, text):
 
21
        print 'echoing',text
 
22
        return text
 
23
 
 
24
    def perspective_error(self):
 
25
        raise DefinedError("exception!")
 
26
 
 
27
    def logout(self):
 
28
        print self, "logged out"
 
29
 
 
30
 
 
31
class SimpleRealm:
 
32
    implements(IRealm)
 
33
 
 
34
    def requestAvatar(self, avatarId, mind, *interfaces):
 
35
        if pb.IPerspective in interfaces:
 
36
            avatar = SimplePerspective()
 
37
            return pb.IPerspective, avatar, avatar.logout 
 
38
        else:
 
39
            raise NotImplementedError("no interface")
 
40
 
 
41
 
 
42
def main():
 
43
    from twisted.internet import reactor
 
44
    from twisted.cred.portal import Portal
 
45
    from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse
 
46
    portal = Portal(SimpleRealm())
 
47
    checker = InMemoryUsernamePasswordDatabaseDontUse()
 
48
    checker.addUser("guest", "guest")
 
49
    portal.registerChecker(checker)
 
50
    reactor.listenTCP(pb.portno, pb.PBServerFactory(portal))
 
51
    reactor.run()