Skip to content

Commit d98f646

Browse files
authored
IPv6 support (#262)
* Support IPv6 host addresses * Add missing tests * Integrate feedback review to make it generally more stable and remove redundancies * Remove unused import * Fix formatting
1 parent 981015c commit d98f646

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

ollama/_client.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ipaddress
12
import os
23
import io
34
import json
@@ -995,6 +996,28 @@ def _parse_host(host: Optional[str]) -> str:
995996
'https://example.com:56789/path'
996997
>>> _parse_host('example.com:56789/path/')
997998
'http://example.com:56789/path'
999+
>>> _parse_host('[0001:002:003:0004::1]')
1000+
'http://[0001:002:003:0004::1]:11434'
1001+
>>> _parse_host('[0001:002:003:0004::1]:56789')
1002+
'http://[0001:002:003:0004::1]:56789'
1003+
>>> _parse_host('http://[0001:002:003:0004::1]')
1004+
'http://[0001:002:003:0004::1]:80'
1005+
>>> _parse_host('https://[0001:002:003:0004::1]')
1006+
'https://[0001:002:003:0004::1]:443'
1007+
>>> _parse_host('https://[0001:002:003:0004::1]:56789')
1008+
'https://[0001:002:003:0004::1]:56789'
1009+
>>> _parse_host('[0001:002:003:0004::1]/')
1010+
'http://[0001:002:003:0004::1]:11434'
1011+
>>> _parse_host('[0001:002:003:0004::1]:56789/')
1012+
'http://[0001:002:003:0004::1]:56789'
1013+
>>> _parse_host('[0001:002:003:0004::1]/path')
1014+
'http://[0001:002:003:0004::1]:11434/path'
1015+
>>> _parse_host('[0001:002:003:0004::1]:56789/path')
1016+
'http://[0001:002:003:0004::1]:56789/path'
1017+
>>> _parse_host('https://[0001:002:003:0004::1]:56789/path')
1018+
'https://[0001:002:003:0004::1]:56789/path'
1019+
>>> _parse_host('[0001:002:003:0004::1]:56789/path/')
1020+
'http://[0001:002:003:0004::1]:56789/path'
9981021
"""
9991022

10001023
host, port = host or '', 11434
@@ -1010,6 +1033,13 @@ def _parse_host(host: Optional[str]) -> str:
10101033
host = split.hostname or '127.0.0.1'
10111034
port = split.port or port
10121035

1036+
# Fix missing square brackets for IPv6 from urlsplit
1037+
try:
1038+
if isinstance(ipaddress.ip_address(host), ipaddress.IPv6Address):
1039+
host = f'[{host}]'
1040+
except ValueError:
1041+
...
1042+
10131043
if path := split.path.strip('/'):
10141044
return f'{scheme}://{host}:{port}/{path}'
10151045

0 commit comments

Comments
 (0)