~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to doc/examples/pb_exceptions.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-01-16 19:56:10 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060116195610-ykmxbia4mnnod9o2
Tags: 2.1.0-0ubuntu2
debian/copyright: Include copyright for python 2.3; some 2.3 files
are included in the upstream tarball, but not in the binary packages.

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()