blob: e6168e2da6c278f749f6040a358ffd1d246ae2ea [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:
Mark Slee22974602007-07-06 22:20:19 +000033 res0 = socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM)
34 for res in res0:
35 self.handle = socket.socket(res[0], res[1])
36 try:
37 self.handle.connect(res[4])
38 except socket.error, e:
39 if res is not res0[-1]:
40 continue
41 else:
42 raise e
43 break
Mark Slee92195ae2007-02-21 05:16:30 +000044 except socket.error, e:
Mark Slee76791962007-03-14 02:47:35 +000045 raise TTransportException(TTransportException.NOT_OPEN, 'Could not connect to %s:%d' % (self.host, self.port))
Mark Sleecde2b612006-09-03 21:13:07 +000046
47 def close(self):
Mark Sleed788b2e2006-09-07 01:26:35 +000048 if self.handle != None:
49 self.handle.close()
50 self.handle = None
Mark Sleecde2b612006-09-03 21:13:07 +000051
Mark Sleecde2b612006-09-03 21:13:07 +000052 def read(self, sz):
53 buff = self.handle.recv(sz)
Mark Sleec9676562006-09-05 17:34:52 +000054 if len(buff) == 0:
Mark Slee4f0fed62006-10-02 17:50:08 +000055 raise TTransportException('TSocket read 0 bytes')
Mark Sleecde2b612006-09-03 21:13:07 +000056 return buff
57
58 def write(self, buff):
Mark Sleec9676562006-09-05 17:34:52 +000059 sent = 0
60 have = len(buff)
61 while sent < have:
62 plus = self.handle.send(buff)
63 if plus == 0:
Mark Slee92195ae2007-02-21 05:16:30 +000064 raise TTransportException('TSocket sent 0 bytes')
Mark Sleec9676562006-09-05 17:34:52 +000065 sent += plus
66 buff = buff[plus:]
Mark Sleecde2b612006-09-03 21:13:07 +000067
68 def flush(self):
69 pass
Mark Sleec9676562006-09-05 17:34:52 +000070
71class TServerSocket(TServerTransportBase):
72
73 """Socket implementation of TServerTransport base."""
74
75 def __init__(self, port):
76 self.port = port
77 self.handle = None
78
79 def listen(self):
Mark Slee22974602007-07-06 22:20:19 +000080 res0 = socket.getaddrinfo(None, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM)
81 for res in res0:
82 if res[0] is socket.AF_INET6 or res is res0[-1]:
83 break
84
85 self.handle = socket.socket(res[0], res[1])
Mark Slee4f0fed62006-10-02 17:50:08 +000086 self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
87 if hasattr(self.handle, 'set_timeout'):
88 self.handle.set_timeout(None)
Mark Slee22974602007-07-06 22:20:19 +000089 self.handle.bind(res[4])
Mark Sleec9676562006-09-05 17:34:52 +000090 self.handle.listen(128)
91
92 def accept(self):
93 (client, addr) = self.handle.accept()
94 result = TSocket()
Mark Slee4f0fed62006-10-02 17:50:08 +000095 result.setHandle(client)
Mark Sleec9676562006-09-05 17:34:52 +000096 return result
97
98 def close(self):
99 self.handle.close()
100 self.handle = None