def trim_to_preferred_protocols(hosts_and_urls):
    """ remove all but http and ftp URLs,
    and if both http and ftp are offered,
    leave only http. Return [(hostid, url), ...] """
    results = []
    try_protocols = ('https', 'http', 'ftp')
    for (hostid, hcurls) in hosts_and_urls:
        protocols = {}
        url = None
        for hcurl in hcurls:
            for p in try_protocols:
                if hcurl.startswith(p+':'):
                    protocols[p] = hcurl

        for p in try_protocols:
            if p in protocols:
                url = protocols[p]
                break

        if url is not None:
            results.append((hostid, url))
    return results