~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to doc/examples/ampserver.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

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 = numerator / denominator
 
25
        print 'Divided: %d / %d = %d' % (numerator, denominator, total)
 
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()