THRIFT-154. python: Make THttpClient take a URL in its constructor
Support https in the process.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@758532 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/py/src/transport/THttpClient.py b/lib/py/src/transport/THttpClient.py
index f577192..a12fdcd 100644
--- a/lib/py/src/transport/THttpClient.py
+++ b/lib/py/src/transport/THttpClient.py
@@ -7,21 +7,47 @@
from TTransport import *
from cStringIO import StringIO
+import urlparse
import httplib
+import warnings
class THttpClient(TTransportBase):
"""Http implementation of TTransport base."""
- def __init__(self, host, port, uri):
- self.host = host
- self.port = port
- self.uri = uri
+ def __init__(self, uri_or_host, port=None, path=None):
+ """THttpClient supports two different types constructor parameters.
+
+ THttpClient(host, port, path) - deprecated
+ THttpClient(uri)
+
+ Only the second supports https."""
+
+ if port is not None:
+ warnings.warn("Please use the THttpClient('http://host:port/path') syntax", DeprecationWarning, stacklevel=2)
+ self.host = uri_or_host
+ self.port = port
+ assert path
+ self.path = path
+ self.scheme = 'http'
+ else:
+ parsed = urlparse.urlparse(uri_or_host)
+ self.scheme = parsed.scheme
+ assert self.scheme in ('http', 'https')
+ if self.scheme == 'http':
+ self.port = parsed.port or httplib.HTTP_PORT
+ elif self.scheme == 'https':
+ self.port = parsed.port or httplib.HTTPS_PORT
+ self.host = parsed.hostname
+ self.path = parsed.path
self.__wbuf = StringIO()
self.__http = None
def open(self):
- self.__http = httplib.HTTP(self.host, self.port)
+ if self.scheme == 'http':
+ self.__http = httplib.HTTP(self.host, self.port)
+ else:
+ self.__http = httplib.HTTPS(self.host, self.port)
def close(self):
self.__http.close()
@@ -46,7 +72,7 @@
self.__wbuf = StringIO()
# HTTP request
- self.__http.putrequest('POST', self.uri)
+ self.__http.putrequest('POST', self.path)
# Write headers
self.__http.putheader('Host', self.host)