Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / examples / pb_exceptions.py
1
2 from twisted.python import util
3 from twisted.spread import pb
4 from twisted.cred import portal, checkers, credentials
5
6 class Avatar(pb.Avatar):
7     def perspective_exception(self, x):
8         return x / 0
9
10 class Realm:
11     def requestAvatar(self, interface, mind, *interfaces):
12         if pb.IPerspective in interfaces:
13             return pb.IPerspective, Avatar(), lambda: None
14
15 def cbLogin(avatar):
16     avatar.callRemote("exception", 10).addCallback(str).addCallback(util.println)
17
18 def ebLogin(failure):
19     print failure
20
21 def main():
22     c = checkers.InMemoryUsernamePasswordDatabaseDontUse(user="pass")
23     p = portal.Portal(Realm(), [c])
24     server = pb.PBServerFactory(p)
25     server.unsafeTracebacks = True
26     client = pb.PBClientFactory()
27     login = client.login(credentials.UsernamePassword("user", "pass"))
28     login.addCallback(cbLogin).addErrback(ebLogin).addBoth(lambda: reactor.stop())
29
30     from twisted.internet import reactor
31     p = reactor.listenTCP(0, server)
32     c = reactor.connectTCP('127.0.0.1', p.getHost().port, client)
33     reactor.run()
34
35 if __name__ == '__main__':
36     main()