~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/examples/pb_exceptions.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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()