blob: 280d6aa6f66b9c3fa26b6a471de841e3d357343f [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)
Mark Sleecde2b612006-09-03 21:13:07 +000030
31 def open(self):
Mark Slee92195ae2007-02-21 05:16:30 +000032 try:
33 self.handle = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
34 self.handle.connect((self.host, self.port))
35 except socket.error, e:
Mark Slee76791962007-03-14 02:47:35 +000036 raise TTransportException(TTransportException.NOT_OPEN, 'Could not connect to %s:%d' % (self.host, self.port))
Mark Sleecde2b612006-09-03 21:13:07 +000037
38 def close(self):
Mark Sleed788b2e2006-09-07 01:26:35 +000039 if self.handle != None:
40 self.handle.close()
41 self.handle = None
Mark Sleecde2b612006-09-03 21:13:07 +000042
Mark Sleecde2b612006-09-03 21:13:07 +000043 def read(self, sz):
44 buff = self.handle.recv(sz)
Mark Sleec9676562006-09-05 17:34:52 +000045 if len(buff) == 0:
Mark Slee4f0fed62006-10-02 17:50:08 +000046 raise TTransportException('TSocket read 0 bytes')
Mark Sleecde2b612006-09-03 21:13:07 +000047 return buff
48
49 def write(self, buff):
Mark Sleec9676562006-09-05 17:34:52 +000050 sent = 0
51 have = len(buff)
52 while sent < have:
53 plus = self.handle.send(buff)
54 if plus == 0:
Mark Slee92195ae2007-02-21 05:16:30 +000055 raise TTransportException('TSocket sent 0 bytes')
Mark Sleec9676562006-09-05 17:34:52 +000056 sent += plus
57 buff = buff[plus:]
Mark Sleecde2b612006-09-03 21:13:07 +000058
59 def flush(self):
60 pass
Mark Sleec9676562006-09-05 17:34:52 +000061
62class TServerSocket(TServerTransportBase):
63
64 """Socket implementation of TServerTransport base."""
65
66 def __init__(self, port):
67 self.port = port
68 self.handle = None
69
70 def listen(self):
71 self.handle = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Mark Slee4f0fed62006-10-02 17:50:08 +000072 self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
73 if hasattr(self.handle, 'set_timeout'):
74 self.handle.set_timeout(None)
Mark Sleec9676562006-09-05 17:34:52 +000075 self.handle.bind(('', self.port))
76 self.handle.listen(128)
77
78 def accept(self):
79 (client, addr) = self.handle.accept()
80 result = TSocket()
Mark Slee4f0fed62006-10-02 17:50:08 +000081 result.setHandle(client)
Mark Sleec9676562006-09-05 17:34:52 +000082 return result
83
84 def close(self):
85 self.handle.close()
86 self.handle = None