[go: nahoru, domu]

Skip to content

Commit

Permalink
Fix SmartConnect()'s handling of IPv6 address with square brackets
Browse files Browse the repository at this point in the history
Closes #978, closes #1053
  • Loading branch information
ddraganov committed Nov 17, 2023
1 parent c61dc2b commit 8fe0812
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions pyVim/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,7 @@ def Connect(host='localhost',
*** Deprecated: Use tokenType instead ***
@type mechanism: string
"""
try:
info = re.match(_rx, host)
if info is not None:
host = info.group(1)
if host[0] == '[':
host = info.group(1)[1:-1]
if info.group(2) is not None:
port = int(info.group(2)[1:])
except ValueError as ve:
pass

host, port = parse_hostport(host, port)
sslContext = getSslContext(host, sslContext, disableSslCertValidation)

if namespace:
Expand Down Expand Up @@ -839,6 +829,7 @@ def SmartStubAdapter(host='localhost',
if preferredApiVersions is None:
preferredApiVersions = GetServiceVersions('vim25')

host, port = parse_hostport(host, port)
sslContext = getSslContext(host, sslContext, disableSslCertValidation)

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

host, port = parse_hostport(host, port)
sslContext = getSslContext(host, sslContext, disableSslCertValidation)

supportedVersion = __FindSupportedVersion(protocol, host, port, path,
Expand Down Expand Up @@ -1058,3 +1050,18 @@ def IsManagedHost():
except Exception as e:
# connect to local server will be refused when host managed by vCenter
return True


def parse_hostport(host, port):
try:
info = re.match(_rx, host)
if info is not None:
host = info.group(1)
if host[0] == '[':
host = info.group(1)[1:-1]
if info.group(2) is not None:
port = int(info.group(2)[1:])
except ValueError as ve:
pass

return host, port

0 comments on commit 8fe0812

Please sign in to comment.