diff --git a/docker/clientbase.py b/docker/clientbase.py index ce52ffa7..0ccab65f 100644 --- a/docker/clientbase.py +++ b/docker/clientbase.py @@ -1,5 +1,6 @@ import json import struct +import sys import requests import requests.exceptions @@ -31,7 +32,7 @@ class ClientBase(requests.Session): self._auth_configs = auth.load_config() - base_url = utils.parse_host(base_url) + base_url = utils.parse_host(base_url, sys.platform) if base_url.startswith('http+unix://'): self._custom_adapter = unixconn.UnixAdapter(base_url, timeout) self.mount('http+docker://', self._custom_adapter) diff --git a/docker/utils/utils.py b/docker/utils/utils.py index 8dc726b3..57864cfd 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -272,11 +272,15 @@ def parse_repository_tag(repo): # fd:// protocol unsupported (for obvious reasons) # Added support for http and https # Protocol translation: tcp -> http, unix -> http+unix -def parse_host(addr): +def parse_host(addr, platform=None): proto = "http+unix" host = DEFAULT_HTTP_HOST port = None path = '' + + if not addr and platform == 'win32': + addr = '{0}:{1}'.format(DEFAULT_HTTP_HOST, 2375) + if not addr or addr.strip() == 'unix://': return DEFAULT_UNIX_SOCKET diff --git a/tests/utils_test.py b/tests/utils_test.py index a668cbaa..7ce888b5 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -79,8 +79,6 @@ class UtilsTest(base.BaseTestCase): 'tcp://:7777': 'http://127.0.0.1:7777', 'http://:7777': 'http://127.0.0.1:7777', 'https://kokia.jp:2375': 'https://kokia.jp:2375', - '': 'http+unix://var/run/docker.sock', - None: 'http+unix://var/run/docker.sock', 'unix:///var/run/docker.sock': 'http+unix:///var/run/docker.sock', 'unix://': 'http+unix://var/run/docker.sock', 'somehost.net:80/service/swarm': ( @@ -90,10 +88,20 @@ class UtilsTest(base.BaseTestCase): for host in invalid_hosts: with pytest.raises(DockerException): - parse_host(host) + parse_host(host, None) for host, expected in valid_hosts.items(): - self.assertEqual(parse_host(host), expected, msg=host) + self.assertEqual(parse_host(host, None), expected, msg=host) + + def test_parse_host_empty_value(self): + unix_socket = 'http+unix://var/run/docker.sock' + tcp_port = 'http://127.0.0.1:2375' + + for val in [None, '']: + for platform in ['darwin', 'linux2', None]: + assert parse_host(val, platform) == unix_socket + + assert parse_host(val, 'win32') == tcp_port def test_kwargs_from_env_empty(self): os.environ.update(DOCKER_HOST='',