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