Skip to content

Commit 75920ce

Browse files
oclykebednar
andauthored
fix: support ipv6 hosts (#132)
Co-authored-by: Jakub Bednář <jakub.bednar@gmail.com>
1 parent 8ac3c88 commit 75920ce

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
# 0.17.0 [unreleased]
44

5+
### Bug Fixes
6+
7+
1. [#132](https://github.com/InfluxCommunity/influxdb3-python/pull/132): Fixes support for IPv6 addresses in the `host` parameter of the client. The client now correctly handles IPv6 addresses by enclosing them in square brackets.
8+
59
### CI
610

711
1. [#164](https://github.com/InfluxCommunity/influxdb3-python/pull/164): Fix pipelines not downloading the correct python images.

influxdb_client_3/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
PointSettings, DefaultWriteOptions, WriteType
1616
from influxdb_client_3.write_client.domain.write_precision import WritePrecision
1717

18+
from ipaddress import IPv6Address, AddressValueError
19+
1820
polars = importlib.util.find_spec("polars") is not None
1921

2022
INFLUX_HOST = "INFLUX_HOST"
@@ -266,6 +268,12 @@ def __init__(
266268
hostname = parsed_url.hostname if parsed_url.hostname else host
267269
port = parsed_url.port if parsed_url.port else 443
268270

271+
try:
272+
IPv6Address(hostname)
273+
hostname = f"[{hostname}]"
274+
except AddressValueError:
275+
pass
276+
269277
# Construct the clients using the parsed values
270278
if write_port_overwrite is not None:
271279
port = write_port_overwrite

tests/test_influxdb_client_3.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,30 @@ async def test_query_async(self):
135135
assert {'data': 'sql_query', 'reference': query, 'value': -1.0} in result_list
136136
assert {'data': 'query_type', 'reference': 'sql', 'value': -1.0} in result_list
137137

138+
@asyncio_run
139+
async def test_query_async_ipv6(self):
140+
with ConstantFlightServer() as server:
141+
client = InfluxDBClient3(
142+
host=f"http://[::1]:{server.port}",
143+
org="my_org",
144+
database="my_db",
145+
token="my_token",
146+
)
147+
148+
query = "SELECT * FROM my_data"
149+
150+
table = await client.query_async(query=query, language="sql")
151+
152+
result_list = table.to_pylist()
153+
154+
cd = ConstantData()
155+
for item in cd.to_list():
156+
assert item in result_list
157+
158+
assert {'data': 'database', 'reference': 'my_db', 'value': -1.0} in result_list
159+
assert {'data': 'sql_query', 'reference': query, 'value': -1.0} in result_list
160+
assert {'data': 'query_type', 'reference': 'sql', 'value': -1.0} in result_list
161+
138162
def test_write_api_custom_options_no_error(self):
139163
write_options = WriteOptions(write_type=WriteType.batching)
140164
write_client_option = {'write_options': write_options}
@@ -352,6 +376,5 @@ def test_get_version_fail(self):
352376
host=f'http://{server.host}:{server.port}', org="ORG", database="DB", token="TOKEN"
353377
).get_server_version()
354378

355-
356379
if __name__ == '__main__':
357380
unittest.main()

0 commit comments

Comments
 (0)