blob: 7b39b63309940df9ec7bb6d7752f384a61ba62b9 [file] [log] [blame]
Mark Sleebd8b9912007-02-27 20:17:00 +00001from TTransport import *
2from cStringIO import StringIO
3
4import httplib
5
6class THttpClient(TTransportBase):
7
8 """Http implementation of TTransport base."""
9
10 def __init__(self, host, port, uri):
11 self.host = host
12 self.port = port
13 self.uri = uri
14 self.__wbuf = StringIO()
15 self.__http = None
16
17 def open(self):
18 self.__http = httplib.HTTP(self.host, self.port)
19
20 def close(self):
21 self.__http.close()
22 self.__http = None
23
24 def isOpen(self):
25 return self.__http != None
26
27 def read(self, sz):
28 return self.__http.file.read(sz)
29
30 def write(self, buf):
31 self.__wbuf.write(buf)
32
33 def flush(self):
34 # Pull data out of buffer
35 data = self.__wbuf.getvalue()
36 self.__wbuf = StringIO()
37
38 # HTTP request
39 self.__http.putrequest('POST', self.uri)
40
41 # Write headers
42 self.__http.putheader('Host', self.host)
43 self.__http.putheader('Content-Type', 'application/x-thrift')
44 self.__http.putheader('Content-Length', str(len(data)))
45 self.__http.endheaders()
46
47 # Write payload
48 self.__http.send(data)
49
50 # Get reply to flush the request
51 self.code, self.message, self.headers = self.__http.getreply()