def valid_host_with_port(hostport):
    """
    matches hostname or an IP, optionally with a port
    """
    host, port = hostport.rsplit(":", 1) if ":" in hostport else (hostport, None)

    # first, validate host or IP
    if not valid_ipv4(host) and not valid_host(host):
        return False

    # now, validate port
    if port is not None and not valid_port(port):
        return False

    return True
matches hostname or an IP, optionally with a port