Skip to content

Commit 8fe0812

Browse files
author
ddraganov
committed
Fix SmartConnect()'s handling of IPv6 address with square brackets
Closes #978, closes #1053
1 parent c61dc2b commit 8fe0812

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

pyVim/connect.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -283,17 +283,7 @@ def Connect(host='localhost',
283283
*** Deprecated: Use tokenType instead ***
284284
@type mechanism: string
285285
"""
286-
try:
287-
info = re.match(_rx, host)
288-
if info is not None:
289-
host = info.group(1)
290-
if host[0] == '[':
291-
host = info.group(1)[1:-1]
292-
if info.group(2) is not None:
293-
port = int(info.group(2)[1:])
294-
except ValueError as ve:
295-
pass
296-
286+
host, port = parse_hostport(host, port)
297287
sslContext = getSslContext(host, sslContext, disableSslCertValidation)
298288

299289
if namespace:
@@ -839,6 +829,7 @@ def SmartStubAdapter(host='localhost',
839829
if preferredApiVersions is None:
840830
preferredApiVersions = GetServiceVersions('vim25')
841831

832+
host, port = parse_hostport(host, port)
842833
sslContext = getSslContext(host, sslContext, disableSslCertValidation)
843834

844835
supportedVersion = __FindSupportedVersion('https' if port > 0 else 'http',
@@ -964,6 +955,7 @@ def SmartConnect(protocol='https',
964955
if preferredApiVersions is None:
965956
preferredApiVersions = GetServiceVersions('vim25')
966957

958+
host, port = parse_hostport(host, port)
967959
sslContext = getSslContext(host, sslContext, disableSslCertValidation)
968960

969961
supportedVersion = __FindSupportedVersion(protocol, host, port, path,
@@ -1058,3 +1050,18 @@ def IsManagedHost():
10581050
except Exception as e:
10591051
# connect to local server will be refused when host managed by vCenter
10601052
return True
1053+
1054+
1055+
def parse_hostport(host, port):
1056+
try:
1057+
info = re.match(_rx, host)
1058+
if info is not None:
1059+
host = info.group(1)
1060+
if host[0] == '[':
1061+
host = info.group(1)[1:-1]
1062+
if info.group(2) is not None:
1063+
port = int(info.group(2)[1:])
1064+
except ValueError as ve:
1065+
pass
1066+
1067+
return host, port

0 commit comments

Comments
 (0)