~soren/nova/iptables-security-groups

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/examples/ampserver.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from twisted.protocols import amp
 
2
 
 
3
class Sum(amp.Command):
 
4
    arguments = [('a', amp.Integer()),
 
5
                 ('b', amp.Integer())]
 
6
    response = [('total', amp.Integer())]
 
7
 
 
8
 
 
9
class Divide(amp.Command):
 
10
    arguments = [('numerator', amp.Integer()),
 
11
                 ('denominator', amp.Integer())]
 
12
    response = [('result', amp.Float())]
 
13
    errors = {ZeroDivisionError: 'ZERO_DIVISION'}
 
14
 
 
15
 
 
16
class Math(amp.AMP):
 
17
    def sum(self, a, b):
 
18
        total = a + b
 
19
        print 'Did a sum: %d + %d = %d' % (a, b, total)
 
20
        return {'total': total}
 
21
    Sum.responder(sum)
 
22
 
 
23
    def divide(self, numerator, denominator):
 
24
        result = float(numerator) / denominator
 
25
        print 'Divided: %d / %d = %f' % (numerator, denominator, result)
 
26
        return {'result': result}
 
27
    Divide.responder(divide)
 
28
 
 
29
 
 
30
def main():
 
31
    from twisted.internet import reactor
 
32
    from twisted.internet.protocol import Factory
 
33
    pf = Factory()
 
34
    pf.protocol = Math
 
35
    reactor.listenTCP(1234, pf)
 
36
    print 'started'
 
37
    reactor.run()
 
38
 
 
39
if __name__ == '__main__':
 
40
    main()