diff --git a/pykodi/kodi.py b/pykodi/kodi.py index 2cbb5eb..6a7cd42 100644 --- a/pykodi/kodi.py +++ b/pykodi/kodi.py @@ -20,6 +20,17 @@ def get_kodi_connection( ) +def _url_host(host): + """Return a host formatted for use in a URL. + + Bare IPv6 literals must be bracketed in URLs (RFC 3986 section 3.2.2), + otherwise their colons are parsed as port separators. + """ + if ":" in host and not host.startswith("["): + return f"[{host}]" + return host + + class KodiConnection: """A connection to Kodi interface.""" @@ -41,7 +52,7 @@ def __init__(self, host, port, username, password, ssl, timeout, session): http_protocol = "https" if ssl else "http" - self._image_url = f"{http_protocol}://{image_auth_string}{host}:{port}/image" + self._image_url = f"{http_protocol}://{image_auth_string}{_url_host(host)}:{port}/image" async def connect(self): """Connect to kodi.""" @@ -86,7 +97,7 @@ def __init__(self, host, port, username, password, ssl, timeout, session): http_protocol = "https" if ssl else "http" - http_url = f"{http_protocol}://{host}:{port}/jsonrpc" + http_url = f"{http_protocol}://{_url_host(host)}:{port}/jsonrpc" self._http_server = jsonrpc_async.Server(http_url, **self._kwargs) @@ -114,7 +125,7 @@ def __init__(self, host, port, ws_port, username, password, ssl, timeout, sessio super().__init__(host, port, username, password, ssl, timeout, session) ws_protocol = "wss" if ssl else "ws" - ws_url = f"{ws_protocol}://{host}:{ws_port}/jsonrpc" + ws_url = f"{ws_protocol}://{_url_host(host)}:{ws_port}/jsonrpc" self._ws_server = jsonrpc_websocket.Server(ws_url, **self._kwargs)