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) |
James Wang | e168d5e | 2007-07-24 23:59:51 +0000 | [diff] [blame] | 30 | else: |
| 31 | raise TTransportException(TTransportException.NOT_OPEN, 'No handle yet in TSocket') |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 32 | |
| 33 | def open(self): |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 34 | try: |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame^] | 35 | res0 = socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 36 | for res in res0: |
| 37 | self.handle = socket.socket(res[0], res[1]) |
| 38 | try: |
| 39 | self.handle.connect(res[4]) |
| 40 | except socket.error, e: |
| 41 | if res is not res0[-1]: |
| 42 | continue |
| 43 | else: |
| 44 | raise e |
| 45 | break |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 46 | except socket.error, e: |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 47 | 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] | 48 | |
| 49 | def close(self): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 50 | if self.handle != None: |
| 51 | self.handle.close() |
| 52 | self.handle = None |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 53 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 54 | def read(self, sz): |
| 55 | buff = self.handle.recv(sz) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 56 | if len(buff) == 0: |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 57 | raise TTransportException('TSocket read 0 bytes') |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 58 | return buff |
| 59 | |
| 60 | def write(self, buff): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 61 | sent = 0 |
| 62 | have = len(buff) |
| 63 | while sent < have: |
| 64 | plus = self.handle.send(buff) |
| 65 | if plus == 0: |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 66 | raise TTransportException('TSocket sent 0 bytes') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 67 | sent += plus |
| 68 | buff = buff[plus:] |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 69 | |
| 70 | def flush(self): |
| 71 | pass |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 72 | |
| 73 | class TServerSocket(TServerTransportBase): |
| 74 | |
| 75 | """Socket implementation of TServerTransport base.""" |
| 76 | |
| 77 | def __init__(self, port): |
| 78 | self.port = port |
| 79 | self.handle = None |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame^] | 80 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 81 | def listen(self): |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame^] | 82 | res0 = socket.getaddrinfo(None, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 83 | for res in res0: |
| 84 | if res[0] is socket.AF_INET6 or res is res0[-1]: |
| 85 | break |
| 86 | |
| 87 | self.handle = socket.socket(res[0], res[1]) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 88 | self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 89 | if hasattr(self.handle, 'set_timeout'): |
| 90 | self.handle.set_timeout(None) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 91 | self.handle.bind(res[4]) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 92 | self.handle.listen(128) |
| 93 | |
| 94 | def accept(self): |
| 95 | (client, addr) = self.handle.accept() |
| 96 | result = TSocket() |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 97 | result.setHandle(client) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 98 | return result |
| 99 | |
| 100 | def close(self): |
| 101 | self.handle.close() |
| 102 | self.handle = None |