Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (c) 2006- Facebook |
| 4 | # Distributed under the Thrift Software License |
| 5 | # |
| 6 | # See accompanying file LICENSE or visit the Thrift site at: |
| 7 | # http://developers.facebook.com/thrift/ |
| 8 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 9 | from TTransport import * |
| 10 | import socket |
| 11 | |
| 12 | class TSocket(TTransportBase): |
| 13 | |
| 14 | """Socket implementation of TTransport base.""" |
| 15 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 16 | def __init__(self, host='localhost', port=9090): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 17 | self.host = host |
Aditya Agarwal | 9bae5e7 | 2007-02-07 02:36:56 +0000 | [diff] [blame] | 18 | self.port = port |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 19 | self.handle = None |
| 20 | |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 21 | def setHandle(self, h): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 22 | self.handle = h |
| 23 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 24 | def isOpen(self): |
Aditya Agarwal | f954f97 | 2007-02-06 01:26:12 +0000 | [diff] [blame] | 25 | return self.handle != None |
| 26 | |
| 27 | def setTimeout(self, ms): |
| 28 | if (self.handle != None): |
| 29 | self.handle.settimeout(ms/1000.00) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 30 | |
| 31 | def open(self): |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 32 | try: |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame^] | 33 | res0 = socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM) |
| 34 | for res in res0: |
| 35 | self.handle = socket.socket(res[0], res[1]) |
| 36 | try: |
| 37 | self.handle.connect(res[4]) |
| 38 | except socket.error, e: |
| 39 | if res is not res0[-1]: |
| 40 | continue |
| 41 | else: |
| 42 | raise e |
| 43 | break |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 44 | except socket.error, e: |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 45 | raise TTransportException(TTransportException.NOT_OPEN, 'Could not connect to %s:%d' % (self.host, self.port)) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 46 | |
| 47 | def close(self): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 48 | if self.handle != None: |
| 49 | self.handle.close() |
| 50 | self.handle = None |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 51 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 52 | def read(self, sz): |
| 53 | buff = self.handle.recv(sz) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 54 | if len(buff) == 0: |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 55 | raise TTransportException('TSocket read 0 bytes') |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 56 | return buff |
| 57 | |
| 58 | def write(self, buff): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 59 | sent = 0 |
| 60 | have = len(buff) |
| 61 | while sent < have: |
| 62 | plus = self.handle.send(buff) |
| 63 | if plus == 0: |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 64 | raise TTransportException('TSocket sent 0 bytes') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 65 | sent += plus |
| 66 | buff = buff[plus:] |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 67 | |
| 68 | def flush(self): |
| 69 | pass |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 70 | |
| 71 | class TServerSocket(TServerTransportBase): |
| 72 | |
| 73 | """Socket implementation of TServerTransport base.""" |
| 74 | |
| 75 | def __init__(self, port): |
| 76 | self.port = port |
| 77 | self.handle = None |
| 78 | |
| 79 | def listen(self): |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame^] | 80 | res0 = socket.getaddrinfo(None, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM) |
| 81 | for res in res0: |
| 82 | if res[0] is socket.AF_INET6 or res is res0[-1]: |
| 83 | break |
| 84 | |
| 85 | self.handle = socket.socket(res[0], res[1]) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 86 | self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 87 | if hasattr(self.handle, 'set_timeout'): |
| 88 | self.handle.set_timeout(None) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame^] | 89 | self.handle.bind(res[4]) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 90 | self.handle.listen(128) |
| 91 | |
| 92 | def accept(self): |
| 93 | (client, addr) = self.handle.accept() |
| 94 | result = TSocket() |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 95 | result.setHandle(client) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 96 | return result |
| 97 | |
| 98 | def close(self): |
| 99 | self.handle.close() |
| 100 | self.handle = None |