From 206e0d609cd6af26ff358a22f92a99794c9b2d45 Mon Sep 17 00:00:00 2001 From: Nick Lang Date: Fri, 18 May 2018 13:52:37 -0600 Subject: [PATCH] handle gzip for python27. Fixes #789 (#793) --- elasticsearch/connection/http_urllib3.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/elasticsearch/connection/http_urllib3.py b/elasticsearch/connection/http_urllib3.py index 8601ad2c..c58944e9 100644 --- a/elasticsearch/connection/http_urllib3.py +++ b/elasticsearch/connection/http_urllib3.py @@ -161,7 +161,13 @@ class Urllib3HttpConnection(Connection): request_headers = request_headers.copy() request_headers.update(headers) if self.http_compress and body: - body = gzip.compress(body) + try: + body = gzip.compress(body) + except AttributeError: + # oops, Python2.7 doesn't have `gzip.compress` let's try + # again + body = gzip.zlib.compress(body) + response = self.pool.urlopen(method, url, body, retries=False, headers=request_headers, **kw) duration = time.time() - start raw_data = response.data.decode('utf-8')