(reactor, ports)
Internal helper for Onion services. Validates an incoming list of
    port mappings and returns a list of strings suitable for passing
    to other onion-services functions.

    Accepts 3 different ways of specifying ports:

      - list of ints: each int is the public port, local port random
      - list of 2-tuples of ints: (pubic, local) ports.
      - list of strings like "80 127.0.0.1:1234"

    This is async in case it needs to ask for a random, unallocated
    local port.
Internal helper for Onion services. Validates an incoming list of
    port mappings and returns a list of strings suitable for passing
    to other onion-services functions.
Internal helper for Onion services . Validates an incoming list of port mappings and returns a list of strings suitable for passing to other onion - services functions .
def _validate_ports(reactor, ports):
    """
    Internal helper for Onion services. Validates an incoming list of
    port mappings and returns a list of strings suitable for passing
    to other onion-services functions.

    Accepts 3 different ways of specifying ports:

      - list of ints: each int is the public port, local port random
      - list of 2-tuples of ints: (pubic, local) ports.
      - list of strings like "80 127.0.0.1:1234"

    This is async in case it needs to ask for a random, unallocated
    local port.
    """
    if not isinstance(ports, (list, tuple)):
        raise ValueError("'ports' must be a list of strings, ints or 2-tuples")

    processed_ports = []
    for port in ports:
        if isinstance(port, (set, list, tuple)):
            if len(port) != 2:
                raise ValueError(
                    "'ports' must contain a single int or a 2-tuple of ints"
                )
            remote, local = port
            try:
                remote = int(remote)
            except ValueError:
                raise ValueError(
                    "'ports' has a tuple with a non-integer "
                    "component: {}".format(port)
                )
            try:
                local = int(local)
            except ValueError:
                if local.startswith('unix:/'):
                    pass
                else:
                    if ':' not in local:
                        raise ValueError(
                            "local port must be either an integer"
                            " or start with unix:/ or be an IP:port"
                        )
                    ip, port = local.split(':')
                    if not _is_non_public_numeric_address(ip):
                        log.msg(
                            "'{}' used as onion port doesn't appear to be a "
                            "local, numeric address".format(ip)
                        )
                processed_ports.append(
                    "{} {}".format(remote, local)
                )
            else:
                processed_ports.append(
                    "{} 127.0.0.1:{}".format(remote, local)
                )

        elif isinstance(port, (six.text_type, str)):
            _validate_single_port_string(port)
            processed_ports.append(port)

        else:
            try:
                remote = int(port)
            except (ValueError, TypeError):
                raise ValueError(
                    "'ports' has a non-integer entry: {}".format(port)
                )
            local = yield available_tcp_port(reactor)
            processed_ports.append(
                "{} 127.0.0.1:{}".format(remote, local)
            )
    defer.returnValue(processed_ports)
def _validate_ports ( reactor , ports ) : if not isinstance ( ports , ( list , tuple ) ) : raise ValueError ( "'ports' must be a list of strings, ints or 2-tuples" ) processed_ports = [ ] for port in ports : if isinstance ( port , ( set , list , tuple ) ) : if len ( port ) != 2 : raise ValueError ( "'ports' must contain a single int or a 2-tuple of ints" ) remote , local = port try : remote = int ( remote ) except ValueError : raise ValueError ( "'ports' has a tuple with a non-integer " "component: {}" . format ( port ) ) try : local = int ( local ) except ValueError : if local . startswith ( 'unix:/' ) : pass else : if ':' not in local : raise ValueError ( "local port must be either an integer" " or start with unix:/ or be an IP:port" ) ip , port = local . split ( ':' ) if not _is_non_public_numeric_address ( ip ) : log . msg ( "'{}' used as onion port doesn't appear to be a " "local, numeric address" . format ( ip ) ) processed_ports . append ( "{} {}" . format ( remote , local ) ) else : processed_ports . append ( "{} 127.0.0.1:{}" . format ( remote , local ) ) elif isinstance ( port , ( six . text_type , str ) ) : _validate_single_port_string ( port ) processed_ports . append ( port ) else : try : remote = int ( port ) except ( ValueError , TypeError ) : raise ValueError ( "'ports' has a non-integer entry: {}" . format ( port ) ) local = yield available_tcp_port ( reactor ) processed_ports . append ( "{} 127.0.0.1:{}" . format ( remote , local ) ) defer . returnValue ( processed_ports )