From bd8b991fa740def63b74a6b54ab746b59c7b3d89 Mon Sep 17 00:00:00 2001 From: Mark Slee Date: Tue, 27 Feb 2007 20:17:00 +0000 Subject: [PATCH] Python HttpClient for Thrift Reviewed By: thrifty goodness for SMC git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665026 13f79535-47bb-0310-9956-ffa450edef68 --- lib/py/src/transport/THttpClient.py | 51 +++++++++++++++++++++++++++++ lib/py/src/transport/__init__.py | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 lib/py/src/transport/THttpClient.py diff --git a/lib/py/src/transport/THttpClient.py b/lib/py/src/transport/THttpClient.py new file mode 100644 index 00000000..7b39b633 --- /dev/null +++ b/lib/py/src/transport/THttpClient.py @@ -0,0 +1,51 @@ +from TTransport import * +from cStringIO import StringIO + +import httplib + +class THttpClient(TTransportBase): + + """Http implementation of TTransport base.""" + + def __init__(self, host, port, uri): + self.host = host + self.port = port + self.uri = uri + self.__wbuf = StringIO() + self.__http = None + + def open(self): + self.__http = httplib.HTTP(self.host, self.port) + + def close(self): + self.__http.close() + self.__http = None + + def isOpen(self): + return self.__http != None + + def read(self, sz): + return self.__http.file.read(sz) + + def write(self, buf): + self.__wbuf.write(buf) + + def flush(self): + # Pull data out of buffer + data = self.__wbuf.getvalue() + self.__wbuf = StringIO() + + # HTTP request + self.__http.putrequest('POST', self.uri) + + # Write headers + self.__http.putheader('Host', self.host) + self.__http.putheader('Content-Type', 'application/x-thrift') + self.__http.putheader('Content-Length', str(len(data))) + self.__http.endheaders() + + # Write payload + self.__http.send(data) + + # Get reply to flush the request + self.code, self.message, self.headers = self.__http.getreply() diff --git a/lib/py/src/transport/__init__.py b/lib/py/src/transport/__init__.py index fb50c8bf..b45e718c 100644 --- a/lib/py/src/transport/__init__.py +++ b/lib/py/src/transport/__init__.py @@ -1 +1 @@ -__all__ = ["TTransport", "TSocket"] +__all__ = ['TTransport', 'TSocket', 'THttpClient'] -- 2.17.1