blob: ee429cd5d22bb64b7356bce19fcbcd2efe17aa1a [file] [log] [blame]
Mark Sleecde2b612006-09-03 21:13:07 +00001from TTransport import *
2import socket
3
4class TSocket(TTransportBase):
5
6 """Socket implementation of TTransport base."""
7
Aditya Agarwal9bae5e72007-02-07 02:36:56 +00008
Mark Sleec9676562006-09-05 17:34:52 +00009 def __init__(self, host='localhost', port=9090):
Mark Sleecde2b612006-09-03 21:13:07 +000010 self.host = host
Aditya Agarwal9bae5e72007-02-07 02:36:56 +000011 self.port = port
Mark Sleecde2b612006-09-03 21:13:07 +000012 self.handle = None
13
Mark Slee4f0fed62006-10-02 17:50:08 +000014 def setHandle(self, h):
Mark Sleec9676562006-09-05 17:34:52 +000015 self.handle = h
16
Mark Sleecde2b612006-09-03 21:13:07 +000017 def isOpen(self):
Aditya Agarwalf954f972007-02-06 01:26:12 +000018 return self.handle != None
19
20 def setTimeout(self, ms):
21 if (self.handle != None):
22 self.handle.settimeout(ms/1000.00)
Mark Sleecde2b612006-09-03 21:13:07 +000023
24 def open(self):
Mark Slee92195ae2007-02-21 05:16:30 +000025 try:
26 self.handle = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
27 self.handle.connect((self.host, self.port))
28 except socket.error, e:
29 raise TTransportException(TTransportException.NOT_OPEN, e.message)
Mark Sleecde2b612006-09-03 21:13:07 +000030
31 def close(self):
Mark Sleed788b2e2006-09-07 01:26:35 +000032 if self.handle != None:
33 self.handle.close()
34 self.handle = None
Mark Sleecde2b612006-09-03 21:13:07 +000035
Mark Sleecde2b612006-09-03 21:13:07 +000036 def read(self, sz):
37 buff = self.handle.recv(sz)
Mark Sleec9676562006-09-05 17:34:52 +000038 if len(buff) == 0:
Mark Slee4f0fed62006-10-02 17:50:08 +000039 raise TTransportException('TSocket read 0 bytes')
Mark Sleecde2b612006-09-03 21:13:07 +000040 return buff
41
42 def write(self, buff):
Mark Sleec9676562006-09-05 17:34:52 +000043 sent = 0
44 have = len(buff)
45 while sent < have:
46 plus = self.handle.send(buff)
47 if plus == 0:
Mark Slee92195ae2007-02-21 05:16:30 +000048 raise TTransportException('TSocket sent 0 bytes')
Mark Sleec9676562006-09-05 17:34:52 +000049 sent += plus
50 buff = buff[plus:]
Mark Sleecde2b612006-09-03 21:13:07 +000051
52 def flush(self):
53 pass
Mark Sleec9676562006-09-05 17:34:52 +000054
55class TServerSocket(TServerTransportBase):
56
57 """Socket implementation of TServerTransport base."""
58
59 def __init__(self, port):
60 self.port = port
61 self.handle = None
62
63 def listen(self):
64 self.handle = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Mark Slee4f0fed62006-10-02 17:50:08 +000065 self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
66 if hasattr(self.handle, 'set_timeout'):
67 self.handle.set_timeout(None)
Mark Sleec9676562006-09-05 17:34:52 +000068 self.handle.bind(('', self.port))
69 self.handle.listen(128)
70
71 def accept(self):
72 (client, addr) = self.handle.accept()
73 result = TSocket()
Mark Slee4f0fed62006-10-02 17:50:08 +000074 result.setHandle(client)
Mark Sleec9676562006-09-05 17:34:52 +000075 return result
76
77 def close(self):
78 self.handle.close()
79 self.handle = None