Skip to content

Commit 64c125d

Browse files
robgilfxdgear
authored andcommitted
Adding GZip support to urllib3 (elastic#704)
* Adding GZip support to urllib3 * Adding compression documentation and example * Convert to lowercase for consistency * Moving header manipulation to __init__() * Validating headers for compression * Moving body compression out of the headers block * Don't compress if there is no body * Infer true
1 parent c354429 commit 64c125d

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

docs/index.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,17 @@ bodies via post::
258258
from elasticsearch import Elasticsearch
259259
es = Elasticsearch(send_get_body_as='POST')
260260

261+
Compression
262+
~~~~~~~~~~~
263+
When using capacity constrained networks (low throughput), it may be handy to enable
264+
compression. This is especially useful when doing bulk loads or inserting large
265+
documents. This will configure compression on the *request*.
266+
::
267+
268+
from elasticsearch import Elasticsearch
269+
es = Elasticsearch(hosts, http_compress = True)
270+
271+
261272
Running on AWS with IAM
262273
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
263274

elasticsearch/connection/http_urllib3.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import urllib3
44
from urllib3.exceptions import ReadTimeoutError, SSLError as UrllibSSLError
55
import warnings
6+
import gzip
67

78
# sentinal value for `verify_certs`.
89
# This is used to detect if a user is passing in a value for `verify_certs`
@@ -62,13 +63,15 @@ class Urllib3HttpConnection(Connection):
6263
host. See https://urllib3.readthedocs.io/en/1.4/pools.html#api for more
6364
information.
6465
:arg headers: any custom http headers to be add to requests
66+
:arg http_compress: Use gzip compression
6567
"""
6668
def __init__(self, host='localhost', port=9200, http_auth=None,
6769
use_ssl=False, verify_certs=VERIFY_CERTS_DEFAULT, ca_certs=None, client_cert=None,
6870
client_key=None, ssl_version=None, ssl_assert_hostname=None,
69-
ssl_assert_fingerprint=None, maxsize=10, headers=None, ssl_context=None, **kwargs):
71+
ssl_assert_fingerprint=None, maxsize=10, headers=None, ssl_context=None, http_compress=False, **kwargs):
7072

7173
super(Urllib3HttpConnection, self).__init__(host=host, port=port, use_ssl=use_ssl, **kwargs)
74+
self.http_compress = http_compress
7275
self.headers = urllib3.make_headers(keep_alive=True)
7376
if http_auth is not None:
7477
if isinstance(http_auth, (tuple, list)):
@@ -80,6 +83,10 @@ def __init__(self, host='localhost', port=9200, http_auth=None,
8083
for k in headers:
8184
self.headers[k.lower()] = headers[k]
8285

86+
if self.http_compress == True:
87+
self.headers.update(urllib3.make_headers(accept_encoding=True))
88+
self.headers.update({'content-encoding': 'gzip'})
89+
8390
self.headers.setdefault('content-type', 'application/json')
8491
pool_class = urllib3.HTTPConnectionPool
8592
kw = {}
@@ -154,6 +161,8 @@ def perform_request(self, method, url, params=None, body=None, timeout=None, ign
154161
if headers:
155162
request_headers = request_headers.copy()
156163
request_headers.update(headers)
164+
if self.http_compress and body:
165+
body = gzip.compress(body)
157166
response = self.pool.urlopen(method, url, body, retries=False, headers=request_headers, **kw)
158167
duration = time.time() - start
159168
raw_data = response.data.decode('utf-8')

test_elasticsearch/test_connection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ def test_ssl_context(self):
3232
)
3333
self.assertTrue(con.use_ssl)
3434

35+
def test_http_compression(self):
36+
con = Urllib3HttpConnection(http_compress=True)
37+
self.assertTrue(con.http_compress)
38+
self.assertEquals(con.headers['content-encoding'], 'gzip')
39+
3540
def test_timeout_set(self):
3641
con = Urllib3HttpConnection(timeout=42)
3742
self.assertEquals(42, con.timeout)

0 commit comments

Comments
 (0)