¡@

Home 

OpenStack Study: network_utils.py

OpenStack Index

**** CubicPower OpenStack Study ****

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))

**** CubicPower OpenStack Study ****

def urlsplit(url, scheme='', allow_fragments=True):

    """Parse a URL using urlparse.urlsplit(), splitting query and fragments.

    This function papers over Python issue9374 when needed.

    The parameters are the same as urlparse.urlsplit.

    """

    scheme, netloc, path, query, fragment = urlutils.urlsplit(

        url, scheme, allow_fragments)

    if allow_fragments and '#' in path:

        path, fragment = path.split('#', 1)

    if '?' in path:

        path, query = path.split('?', 1)

    return urlutils.SplitResult(scheme, netloc, path, query, fragment)