()
return mirrors
Discover available Ubuntu mirrors. (fallback)

    :returns: A set of :class:`.CandidateMirror` objects that have their
              :attr:`~.CandidateMirror.mirror_url` property set and may have
              the :attr:`~.CandidateMirror.last_updated` property set.
    :raises: If no mirrors are discovered an exception is raised.

    This queries :data:`MIRRORS_URL`to discover available Ubuntu mirrors.
    Here's an example run:

    >>> from apt_smart.backends.ubuntu import discover_mirrors_old
    >>> from pprint import pprint
    >>> pprint(discover_mirrors_old())
    set([CandidateMirror(mirror_url='http://archive.ubuntu.com/ubuntu/'),
         CandidateMirror(mirror_url='http://ftp.nluug.nl/os/Linux/distr/ubuntu/'),
         CandidateMirror(mirror_url='http://ftp.snt.utwente.nl/pub/os/linux/ubuntu/'),
         CandidateMirror(mirror_url='http://ftp.tudelft.nl/archive.ubuntu.com/'),
         CandidateMirror(mirror_url='http://mirror.1000mbps.com/ubuntu/'),
         CandidateMirror(mirror_url='http://mirror.amsiohosting.net/archive.ubuntu.com/'),
         CandidateMirror(mirror_url='http://mirror.i3d.net/pub/ubuntu/'),
         CandidateMirror(mirror_url='http://mirror.nforce.com/pub/linux/ubuntu/'),
         CandidateMirror(mirror_url='http://mirror.nl.leaseweb.net/ubuntu/'),
         CandidateMirror(mirror_url='http://mirror.transip.net/ubuntu/ubuntu/'),
         ...])

    It may be super-slow somewhere ( with 100Mbps fibre though ) in the world to access launchpad.net (see below),
    so we have to no longer rely on MIRRORS_URL .

    time curl -o/dev/null 'https://launchpad.net/ubuntu/+archivemirrors'
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
    100  263k  100  263k    0     0   5316      0  0:00:50  0:00:50 --:--:--  6398

    real    0m50.869s
    user    0m0.045s
    sys     0m0.039s

    But it can be a fallback when MIRROR_SELECTION_URL is down.
Discover available Ubuntu mirrors. (fallback)
Discover available Ubuntu mirrors . ( fallback )
def discover_mirrors_old():
    """
    Discover available Ubuntu mirrors. (fallback)

    :returns: A set of :class:`.CandidateMirror` objects that have their
              :attr:`~.CandidateMirror.mirror_url` property set and may have
              the :attr:`~.CandidateMirror.last_updated` property set.
    :raises: If no mirrors are discovered an exception is raised.

    This queries :data:`MIRRORS_URL`to discover available Ubuntu mirrors.
    Here's an example run:

    >>> from apt_smart.backends.ubuntu import discover_mirrors_old
    >>> from pprint import pprint
    >>> pprint(discover_mirrors_old())
    set([CandidateMirror(mirror_url='http://archive.ubuntu.com/ubuntu/'),
         CandidateMirror(mirror_url='http://ftp.nluug.nl/os/Linux/distr/ubuntu/'),
         CandidateMirror(mirror_url='http://ftp.snt.utwente.nl/pub/os/linux/ubuntu/'),
         CandidateMirror(mirror_url='http://ftp.tudelft.nl/archive.ubuntu.com/'),
         CandidateMirror(mirror_url='http://mirror.1000mbps.com/ubuntu/'),
         CandidateMirror(mirror_url='http://mirror.amsiohosting.net/archive.ubuntu.com/'),
         CandidateMirror(mirror_url='http://mirror.i3d.net/pub/ubuntu/'),
         CandidateMirror(mirror_url='http://mirror.nforce.com/pub/linux/ubuntu/'),
         CandidateMirror(mirror_url='http://mirror.nl.leaseweb.net/ubuntu/'),
         CandidateMirror(mirror_url='http://mirror.transip.net/ubuntu/ubuntu/'),
         ...])

    It may be super-slow somewhere ( with 100Mbps fibre though ) in the world to access launchpad.net (see below),
    so we have to no longer rely on MIRRORS_URL .

    time curl -o/dev/null 'https://launchpad.net/ubuntu/+archivemirrors'
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
    100  263k  100  263k    0     0   5316      0  0:00:50  0:00:50 --:--:--  6398

    real    0m50.869s
    user    0m0.045s
    sys     0m0.039s

    But it can be a fallback when MIRROR_SELECTION_URL is down.
    """
    mirrors = set()
    logger.info("Discovering Ubuntu mirrors at %s ..", MIRRORS_URL)
    # Find which country the user is in to get mirrors in that country
    try:
        url = 'https://ipapi.co/json'
        response = fetch_url(url, timeout=2)
        # On py3 response is bytes and json.loads throws TypeError in py3.4 and 3.5,
        # so decode it to str
        if isinstance(response, six.binary_type):
            response = response.decode('utf-8')
        data = json.loads(response)
        country = data['country_name']
        logger.info("Found your location: %s by %s", country, url)
    except Exception:
        url = 'http://ip-api.com/json'
        response = fetch_url(url, timeout=5)
        if isinstance(response, six.binary_type):
            response = response.decode('utf-8')
        data = json.loads(response)
        country = data['country']
        logger.info("Found your location: %s by %s", country, url)

    data = fetch_url(MIRRORS_URL, timeout=70, retry=True)
    soup = BeautifulSoup(data, 'html.parser')
    tables = soup.findAll('table')
    flag = False  # flag is True when find the row's text is that country
    if not tables:
        raise Exception("Failed to locate <table> element in Ubuntu mirror page! (%s)" % MIRRORS_URL)
    else:
        for row in tables[0].findAll("tr"):
            if flag:
                if not row.a:  # End of mirrors located in that country
                    break
                else:
                    for a in row.findAll('a', href=True):
                        # Check if the link looks like a mirror URL.
                        if a['href'].startswith(('http://', 'https://')):
                            mirrors.add(CandidateMirror(mirror_url=a['href']))
            if row.th and row.th.get_text() == country:
                flag = True

    if not mirrors:
        raise Exception("Failed to discover any Ubuntu mirrors! (using %s)" % MIRRORS_URL)
    return mirrors
def discover_mirrors_old ( ) : mirrors = set ( ) logger . info ( "Discovering Ubuntu mirrors at %s .." , MIRRORS_URL ) # Find which country the user is in to get mirrors in that country try : url = 'https://ipapi.co/json' response = fetch_url ( url , timeout = 2 ) # On py3 response is bytes and json.loads throws TypeError in py3.4 and 3.5, # so decode it to str if isinstance ( response , six . binary_type ) : response = response . decode ( 'utf-8' ) data = json . loads ( response ) country = data [ 'country_name' ] logger . info ( "Found your location: %s by %s" , country , url ) except Exception : url = 'http://ip-api.com/json' response = fetch_url ( url , timeout = 5 ) if isinstance ( response , six . binary_type ) : response = response . decode ( 'utf-8' ) data = json . loads ( response ) country = data [ 'country' ] logger . info ( "Found your location: %s by %s" , country , url ) data = fetch_url ( MIRRORS_URL , timeout = 70 , retry = True ) soup = BeautifulSoup ( data , 'html.parser' ) tables = soup . findAll ( 'table' ) flag = False # flag is True when find the row's text is that country if not tables : raise Exception ( "Failed to locate <table> element in Ubuntu mirror page! (%s)" % MIRRORS_URL ) else : for row in tables [ 0 ] . findAll ( "tr" ) : if flag : if not row . a : # End of mirrors located in that country break else : for a in row . findAll ( 'a' , href = True ) : # Check if the link looks like a mirror URL. if a [ 'href' ] . startswith ( ( 'http://' , 'https://' ) ) : mirrors . add ( CandidateMirror ( mirror_url = a [ 'href' ] ) ) if row . th and row . th . get_text ( ) == country : flag = True if not mirrors : raise Exception ( "Failed to discover any Ubuntu mirrors! (using %s)" % MIRRORS_URL ) return mirrors