File: pb_exceptions.py

package info (click to toggle)
twisted 25.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,560 kB
  • sloc: python: 203,171; makefile: 200; sh: 92; javascript: 36; xml: 31
file content (42 lines) | stat: -rw-r--r-- 1,114 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from twisted.cred import checkers, credentials, portal
from twisted.python import util
from twisted.spread import pb


class Avatar(pb.Avatar):
    def perspective_exception(self, x):
        return x / 0


class Realm:
    def requestAvatar(self, interface, mind, *interfaces):
        if pb.IPerspective in interfaces:
            return pb.IPerspective, Avatar(), lambda: None


def cbLogin(avatar):
    avatar.callRemote(b"exception", 10).addCallback(str).addCallback(util.println)


def ebLogin(failure):
    print(failure)


def main():
    c = checkers.InMemoryUsernamePasswordDatabaseDontUse(user=b"pass")
    p = portal.Portal(Realm(), [c])
    server = pb.PBServerFactory(p)
    server.unsafeTracebacks = True
    client = pb.PBClientFactory()
    login = client.login(credentials.UsernamePassword(b"user", b"pass"))
    login.addCallback(cbLogin).addErrback(ebLogin).addBoth(lambda: reactor.stop())

    from twisted.internet import reactor

    p = reactor.listenTCP(0, server)
    c = reactor.connectTCP("127.0.0.1", p.getHost().port, client)
    reactor.run()


if __name__ == "__main__":
    main()