summaryrefslogtreecommitdiffstats
path: root/openstack
diff options
context:
space:
mode:
authorEugene Kirpichov <ekirpichov@gmail.com>2012-09-07 22:56:34 +0000
committerEugene Kirpichov <ekirpichov@gmail.com>2012-09-11 17:50:43 +0000
commit879470b99c0f8f42d50f955defaf43ab47b704cc (patch)
tree0410ba26dc28ef3c72915bddae48aa0c104c06da /openstack
parent1071b9da480d25e7cee556d7f9b483f8ac258ffb (diff)
downloadoslo-879470b99c0f8f42d50f955defaf43ab47b704cc.tar.gz
oslo-879470b99c0f8f42d50f955defaf43ab47b704cc.tar.xz
oslo-879470b99c0f8f42d50f955defaf43ab47b704cc.zip
Added a method for parsing host:port pairs.
The method works correctly with ipv6. An alternative way would be to use urlparse, but urlparse only works correctly starting with Python 2.7, so to be compatible, we have to reimplement this part. The method will be used for parsing Glance API server addresses, RabbitMQ addresses and perhaps other things. Change-Id: Ie5014891c6abcd80681a370d5dd94cb0406f7f61
Diffstat (limited to 'openstack')
-rw-r--r--openstack/common/utils.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/openstack/common/utils.py b/openstack/common/utils.py
index 74c571d..7df3dbc 100644
--- a/openstack/common/utils.py
+++ b/openstack/common/utils.py
@@ -64,6 +64,50 @@ def bool_from_string(subject):
return False
+def parse_host_port(address, default_port=None):
+ """
+ Interpret a string as a host:port pair.
+ An IPv6 address MUST be escaped if accompanied by a port,
+ because otherwise ambiguity ensues: 2001:db8:85a3::8a2e:370:7334
+ means both [2001:db8:85a3::8a2e:370:7334] and
+ [2001:db8:85a3::8a2e:370]:7334.
+
+ >>> parse_host_port('server01:80')
+ ('server01', 80)
+ >>> parse_host_port('server01')
+ ('server01', None)
+ >>> parse_host_port('server01', default_port=1234)
+ ('server01', 1234)
+ >>> parse_host_port('[::1]:80')
+ ('::1', 80)
+ >>> parse_host_port('[::1]')
+ ('::1', None)
+ >>> parse_host_port('[::1]', default_port=1234)
+ ('::1', 1234)
+ >>> parse_host_port('2001:db8:85a3::8a2e:370:7334', default_port=1234)
+ ('2001:db8:85a3::8a2e:370:7334', 1234)
+
+ """
+ if address[0] == '[':
+ # Escaped ipv6
+ _host, _port = address[1:].split(']')
+ host = _host
+ if ':' in _port:
+ port = _port.split(':')[1]
+ else:
+ port = default_port
+ else:
+ if address.count(':') == 1:
+ host, port = address.split(':')
+ else:
+ # 0 means ipv4, >1 means ipv6.
+ # We prohibit unescaped ipv6 addresses with port.
+ host = address
+ port = default_port
+
+ return (host, None if port is None else int(port))
+
+
def execute(*cmd, **kwargs):
"""
Helper method to execute command with optional retry.