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