blob: 1f9c480cfa7185f2ee1e8d1cbc89a892a6daebb3 [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
Aditya Agarwal9bae5e72007-02-07 02:36:56 +000016
Mark Sleec9676562006-09-05 17:34:52 +000017 def __init__(self, host='localhost', port=9090):
Mark Sleecde2b612006-09-03 21:13:07 +000018 self.host = host
Aditya Agarwal9bae5e72007-02-07 02:36:56 +000019 self.port = port
Mark Sleecde2b612006-09-03 21:13:07 +000020 self.handle = None
21
Mark Slee4f0fed62006-10-02 17:50:08 +000022 def setHandle(self, h):
Mark Sleec9676562006-09-05 17:34:52 +000023 self.handle = h
24
Mark Sleecde2b612006-09-03 21:13:07 +000025 def isOpen(self):
Aditya Agarwalf954f972007-02-06 01:26:12 +000026 return self.handle != None
27
28 def setTimeout(self, ms):
29 if (self.handle != None):
30 self.handle.settimeout(ms/1000.00)
Mark Sleecde2b612006-09-03 21:13:07 +000031
32 def open(self):
Mark Slee92195ae2007-02-21 05:16:30 +000033 try:
34 self.handle = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
35 self.handle.connect((self.host, self.port))
36 except socket.error, e:
Mark Sleeb6616fc2007-03-07 05:46:05 +000037 raise TTransportException(TTransportException.NOT_OPEN)
Mark Sleecde2b612006-09-03 21:13:07 +000038
39 def close(self):
Mark Sleed788b2e2006-09-07 01:26:35 +000040 if self.handle != None:
41 self.handle.close()
42 self.handle = None
Mark Sleecde2b612006-09-03 21:13:07 +000043
Mark Sleecde2b612006-09-03 21:13:07 +000044 def read(self, sz):
45 buff = self.handle.recv(sz)
Mark Sleec9676562006-09-05 17:34:52 +000046 if len(buff) == 0:
Mark Slee4f0fed62006-10-02 17:50:08 +000047 raise TTransportException('TSocket read 0 bytes')
Mark Sleecde2b612006-09-03 21:13:07 +000048 return buff
49
50 def write(self, buff):
Mark Sleec9676562006-09-05 17:34:52 +000051 sent = 0
52 have = len(buff)
53 while sent < have:
54 plus = self.handle.send(buff)
55 if plus == 0:
Mark Slee92195ae2007-02-21 05:16:30 +000056 raise TTransportException('TSocket sent 0 bytes')
Mark Sleec9676562006-09-05 17:34:52 +000057 sent += plus
58 buff = buff[plus:]
Mark Sleecde2b612006-09-03 21:13:07 +000059
60 def flush(self):
61 pass
Mark Sleec9676562006-09-05 17:34:52 +000062
63class TServerSocket(TServerTransportBase):
64
65 """Socket implementation of TServerTransport base."""
66
67 def __init__(self, port):
68 self.port = port
69 self.handle = None
70
71 def listen(self):
72 self.handle = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Mark Slee4f0fed62006-10-02 17:50:08 +000073 self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
74 if hasattr(self.handle, 'set_timeout'):
75 self.handle.set_timeout(None)
Mark Sleec9676562006-09-05 17:34:52 +000076 self.handle.bind(('', self.port))
77 self.handle.listen(128)
78
79 def accept(self):
80 (client, addr) = self.handle.accept()
81 result = TSocket()
Mark Slee4f0fed62006-10-02 17:50:08 +000082 result.setHandle(client)
Mark Sleec9676562006-09-05 17:34:52 +000083 return result
84
85 def close(self):
86 self.handle.close()
87 self.handle = None