blob: 0b443441fa5d47a7447f9b0b3a528175d691f827 [file] [log] [blame]
Mark Slee89e2bb82007-03-01 00:20:36 +00001#!/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 Sleecde2b612006-09-03 21:13:07 +00009from TTransport import *
10import socket
11
12class TSocket(TTransportBase):
13
14 """Socket implementation of TTransport base."""
15
Mark Sleec9676562006-09-05 17:34:52 +000016 def __init__(self, host='localhost', port=9090):
Mark Sleecde2b612006-09-03 21:13:07 +000017 self.host = host
Aditya Agarwal9bae5e72007-02-07 02:36:56 +000018 self.port = port
Mark Sleecde2b612006-09-03 21:13:07 +000019 self.handle = None
20
Mark Slee4f0fed62006-10-02 17:50:08 +000021 def setHandle(self, h):
Mark Sleec9676562006-09-05 17:34:52 +000022 self.handle = h
23
Mark Sleecde2b612006-09-03 21:13:07 +000024 def isOpen(self):
Aditya Agarwalf954f972007-02-06 01:26:12 +000025 return self.handle != None
26
27 def setTimeout(self, ms):
28 if (self.handle != None):
29 self.handle.settimeout(ms/1000.00)
James Wange168d5e2007-07-24 23:59:51 +000030 else:
31 raise TTransportException(TTransportException.NOT_OPEN, 'No handle yet in TSocket')
Mark Sleecde2b612006-09-03 21:13:07 +000032
33 def open(self):
Mark Slee92195ae2007-02-21 05:16:30 +000034 try:
Mark Slee256bdc42007-11-27 08:42:19 +000035 res0 = socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG)
Mark Slee22974602007-07-06 22:20:19 +000036 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 Slee92195ae2007-02-21 05:16:30 +000046 except socket.error, e:
Mark Slee76791962007-03-14 02:47:35 +000047 raise TTransportException(TTransportException.NOT_OPEN, 'Could not connect to %s:%d' % (self.host, self.port))
Mark Sleecde2b612006-09-03 21:13:07 +000048
49 def close(self):
Mark Sleed788b2e2006-09-07 01:26:35 +000050 if self.handle != None:
51 self.handle.close()
52 self.handle = None
Mark Sleecde2b612006-09-03 21:13:07 +000053
Mark Sleecde2b612006-09-03 21:13:07 +000054 def read(self, sz):
55 buff = self.handle.recv(sz)
Mark Sleec9676562006-09-05 17:34:52 +000056 if len(buff) == 0:
Mark Slee4f0fed62006-10-02 17:50:08 +000057 raise TTransportException('TSocket read 0 bytes')
Mark Sleecde2b612006-09-03 21:13:07 +000058 return buff
59
60 def write(self, buff):
Mark Sleec9676562006-09-05 17:34:52 +000061 sent = 0
62 have = len(buff)
63 while sent < have:
64 plus = self.handle.send(buff)
65 if plus == 0:
Mark Slee92195ae2007-02-21 05:16:30 +000066 raise TTransportException('TSocket sent 0 bytes')
Mark Sleec9676562006-09-05 17:34:52 +000067 sent += plus
68 buff = buff[plus:]
Mark Sleecde2b612006-09-03 21:13:07 +000069
70 def flush(self):
71 pass
Mark Sleec9676562006-09-05 17:34:52 +000072
73class TServerSocket(TServerTransportBase):
74
75 """Socket implementation of TServerTransport base."""
76
77 def __init__(self, port):
78 self.port = port
79 self.handle = None
Mark Slee256bdc42007-11-27 08:42:19 +000080
Mark Sleec9676562006-09-05 17:34:52 +000081 def listen(self):
Mark Slee256bdc42007-11-27 08:42:19 +000082 res0 = socket.getaddrinfo(None, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG)
Mark Slee22974602007-07-06 22:20:19 +000083 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 Slee4f0fed62006-10-02 17:50:08 +000088 self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
89 if hasattr(self.handle, 'set_timeout'):
90 self.handle.set_timeout(None)
Mark Slee22974602007-07-06 22:20:19 +000091 self.handle.bind(res[4])
Mark Sleec9676562006-09-05 17:34:52 +000092 self.handle.listen(128)
93
94 def accept(self):
95 (client, addr) = self.handle.accept()
96 result = TSocket()
Mark Slee4f0fed62006-10-02 17:50:08 +000097 result.setHandle(client)
Mark Sleec9676562006-09-05 17:34:52 +000098 return result
99
100 def close(self):
101 self.handle.close()
102 self.handle = None