Imported Upstream version 12.1.0
[contrib/python-twisted.git] / doc / core / howto / listings / pb / pb6server.py
1 #!/usr/bin/env python
2
3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
5
6 from zope.interface import implements
7
8 from twisted.spread import pb
9 from twisted.cred import checkers, portal
10 from twisted.internet import reactor
11
12 class MyPerspective(pb.Avatar):
13     def __init__(self, name):
14         self.name = name
15     def perspective_foo(self, arg):
16         print "I am", self.name, "perspective_foo(",arg,") called on", self
17
18 class MyRealm:
19     implements(portal.IRealm)
20     def requestAvatar(self, avatarId, mind, *interfaces):
21         if pb.IPerspective not in interfaces:
22             raise NotImplementedError
23         return pb.IPerspective, MyPerspective(avatarId), lambda:None
24
25 p = portal.Portal(MyRealm())
26 c = checkers.InMemoryUsernamePasswordDatabaseDontUse(user1="pass1", 
27                                                      user2="pass2")
28 p.registerChecker(c)
29 reactor.listenTCP(8800, pb.PBServerFactory(p))
30 reactor.run()