Skip to content

Commit 8006018

Browse files
authored
Support IPv6 braces in Connection.host
1 parent e1f0e8f commit 8006018

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

elasticsearch/connection/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ def __init__(
137137
self.scheme = scheme
138138
self.hostname = host
139139
self.port = port
140-
self.host = "%s://%s" % (scheme, host)
140+
if ":" in host: # IPv6
141+
self.host = "%s://[%s]" % (scheme, host)
142+
else:
143+
self.host = "%s://%s" % (scheme, host)
141144
if self.port is not None:
142145
self.host += ":%s" % self.port
143146
if url_prefix:

test_elasticsearch/test_connection.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ def test_raises_warnings_when_folded(self):
150150

151151
self.assertEqual([str(w.message) for w in warn], ["warning", "folded"])
152152

153+
def test_ipv6_host_and_port(self):
154+
for kwargs, expected_host in [
155+
({"host": "::1"}, "http://[::1]:9200"),
156+
({"host": "::1", "port": 443}, "http://[::1]:443"),
157+
({"host": "::1", "use_ssl": True}, "https://[::1]:9200"),
158+
({"host": "127.0.0.1", "port": 1234}, "http://127.0.0.1:1234"),
159+
({"host": "localhost", "use_ssl": True}, "https://localhost:9200"),
160+
]:
161+
conn = Connection(**kwargs)
162+
assert conn.host == expected_host
163+
153164

154165
class TestUrllib3Connection(TestCase):
155166
def _get_mock_connection(self, connection_params={}, response_body=b"{}"):

0 commit comments

Comments
 (0)