#NEWLINE# def split_host_port( host_str ) : #NEWLINE# #INDENT# '\n    Split host_str into host and port.\n    Can handle IPv4, IPv6, hostname inside or outside brackets.\n\n    Note: If you want to specify a port with IPv6, you definitely\n    should enclose IP address within [].\n\n    :param host_str: Host port string.\n    :type host_str: ``str``\n\n    :return: Hostname (string), port (int) tuple. Raises exception on invalid port.\n    :rtype: ``tuple`` of ``str`` and ``int``\n    ' #NEWLINE# hostname = host_str #NEWLINE# port = None #NEWLINE# if ( is_ipv6 ( host_str ) or is_ipv4 ( host_str ) ) : #NEWLINE# #INDENT# return ( hostname , port ) #NEWLINE# #UNINDENT# match = COMPILED_BRACKET_PATTERN . match ( match ) #NEWLINE# if match : #NEWLINE# #INDENT# LOG . debug ( 'Square bracket style.' ) #NEWLINE# match = COMPILED_HOST_ONLY_IN_BRACKET_PATTERN . match ( host_str ) #NEWLINE# if match : #NEWLINE# #INDENT# hostname = match . group ( ) . strip ( '[]' ) #NEWLINE# return ( hostname , port ) #NEWLINE# #UNINDENT# ( hostname , separator , port ) = hostname . rpartition ( ':' ) #NEWLINE# try : #NEWLINE# #INDENT# LOG . debug ( ( 'host_str: %s, hostname: %s port: %s' % ( host_str , hostname , port ) ) ) #NEWLINE# port = int ( port ) #NEWLINE# hostname = hostname . strip ( '[]' ) #NEWLINE# return ( hostname , port ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# raise Exception ( ( 'Invalid port %s specified.' % port ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# LOG . debug ( ( 'Non-bracket address. host_str: %s' % host_str ) ) #NEWLINE# if ( ':' in host_str ) : #NEWLINE# #INDENT# LOG . debug ( 'Non-bracket with port.' ) #NEWLINE# ( hostname , separator , port ) = hostname . rpartition ( ':' ) #NEWLINE# try : #NEWLINE# #INDENT# port = int ( port ) #NEWLINE# return ( hostname , port ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# raise Exception ( ( 'Invalid port %s specified.' % port ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return ( hostname , port )