~divmod-dev/divmod.org/trunk

« back to all changes in this revision

Viewing changes to Epsilon/doc/listings/amp/amp_auth_server.py

  • Committer: Jean-Paul Calderone
  • Date: 2014-06-29 20:33:04 UTC
  • mfrom: (2749.1.1 remove-epsilon-1325289)
  • Revision ID: exarkun@twistedmatrix.com-20140629203304-gdkmbwl1suei4m97
mergeĀ lp:~exarkun/divmod.org/remove-epsilon-1325289

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2008 Divmod.  See LICENSE for details.
2
 
 
3
 
"""
4
 
An AMP server which requires authentication of its clients before exposing an
5
 
addition command.
6
 
"""
7
 
 
8
 
from sys import stdout
9
 
 
10
 
from twisted.python.log import startLogging, msg
11
 
from twisted.internet import reactor
12
 
from twisted.cred.portal import Portal
13
 
from twisted.protocols.amp import IBoxReceiver, Command, Integer, AMP
14
 
 
15
 
from epsilon.ampauth import CredAMPServerFactory, OneTimePadChecker
16
 
 
17
 
 
18
 
class Add(Command):
19
 
    """
20
 
    An example of an application-defined command which should be made available
21
 
    to clients after they successfully authenticate.
22
 
    """
23
 
    arguments = [("left", Integer()),
24
 
                 ("right", Integer())]
25
 
 
26
 
    response = [("sum", Integer())]
27
 
 
28
 
 
29
 
 
30
 
class Adder(AMP):
31
 
    """
32
 
    An example of an application-defined AMP protocol, the responders defined
33
 
    by which should only be available to clients after they have successfully
34
 
    authenticated.
35
 
    """
36
 
    def __init__(self, avatarId):
37
 
        AMP.__init__(self)
38
 
        self.avatarId = avatarId
39
 
 
40
 
 
41
 
    @Add.responder
42
 
    def add(self, left, right):
43
 
        msg("Adding %d to %d for %s" % (left, right, self.avatarId))
44
 
        return {'sum': left + right}
45
 
 
46
 
 
47
 
 
48
 
class AdditionRealm(object):
49
 
    """
50
 
    An example of an application-defined realm.
51
 
    """
52
 
    def requestAvatar(self, avatarId, mind, *interfaces):
53
 
        """
54
 
        Create Adder avatars for any IBoxReceiver request.
55
 
        """
56
 
        if IBoxReceiver in interfaces:
57
 
            return (IBoxReceiver, Adder(avatarId), lambda: None)
58
 
        raise NotImplementedError()
59
 
 
60
 
 
61
 
 
62
 
def main():
63
 
    """
64
 
    Start the AMP server and the reactor.
65
 
    """
66
 
    startLogging(stdout)
67
 
    checker = OneTimePadChecker({'pad': 0})
68
 
    realm = AdditionRealm()
69
 
    factory = CredAMPServerFactory(Portal(realm, [checker]))
70
 
    reactor.listenTCP(7805, factory)
71
 
    reactor.run()
72
 
 
73
 
 
74
 
if __name__ == '__main__':
75
 
    main()
76