Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 1 | from thrift.Thrift import TProcessor |
| 2 | from thrift.transport import TTransport |
| 3 | |
| 4 | class TServer: |
| 5 | |
| 6 | """Base interface for a server, which must have a run method.""" |
| 7 | |
| 8 | def __init__(self, proc): |
| 9 | self.processor = proc |
| 10 | |
| 11 | def run(self): |
| 12 | pass |
| 13 | |
| 14 | class TSimpleServer(TServer): |
| 15 | |
| 16 | """Simple single-threaded server that just pumps around one transport.""" |
| 17 | |
| 18 | def __init__(self, proc, trans): |
| 19 | TServer.__init__(self, proc) |
| 20 | self.transport = trans |
| 21 | |
| 22 | def run(self): |
| 23 | self.transport.listen() |
| 24 | while True: |
| 25 | client = TTransport.TBufferedTransport(self.transport.accept()) |
| 26 | try: |
| 27 | while True: |
| 28 | self.processor.process(client, client) |
| 29 | except Exception, x: |
| 30 | print x |
| 31 | print 'Client died.' |
| 32 | client.close() |