()
return mirrors
Discover available Debian mirrors by querying :data:`MIRRORS_URL`.

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

    An example run:

    >>> from apt_smart.backends.debian import discover_mirrors
    >>> from pprint import pprint
    >>> pprint(discover_mirrors())
    set([CandidateMirror(mirror_url='http://ftp.at.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.au.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.be.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.bg.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.br.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.by.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.ca.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.ch.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.cn.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.cz.debian.org/debian/'),
         ...])
Discover available Debian mirrors by querying :data:`MIRRORS_URL`.
Discover available Debian mirrors by querying : data : MIRRORS_URL .
def discover_mirrors():
    """
    Discover available Debian mirrors by querying :data:`MIRRORS_URL`.

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

    An example run:

    >>> from apt_smart.backends.debian import discover_mirrors
    >>> from pprint import pprint
    >>> pprint(discover_mirrors())
    set([CandidateMirror(mirror_url='http://ftp.at.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.au.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.be.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.bg.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.br.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.by.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.ca.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.ch.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.cn.debian.org/debian/'),
         CandidateMirror(mirror_url='http://ftp.cz.debian.org/debian/'),
         ...])
    """
    timer = Timer()
    logger.info("Discovering Debian 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=20, 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
    mirrors = set()
    if not tables:
        raise Exception("Failed to locate <table> element in Debian mirror page! (%s)" % MIRRORS_URL)
    else:
        for row in tables[1].findAll("tr"):  # tables[1] organises mirrors by country.
            if flag:
                if not row.a:  # End of mirrors located in that country
                    break
                else:
                    mirrors.add(CandidateMirror(mirror_url=row.a['href']))
            if row.get_text() == country:
                flag = True

    if len(mirrors) < 3:  # Too few, add tables[0] which contains Primary Debian mirror sites all around the world.
        mirrors.add(CandidateMirror(mirror_url=a['href']) for a in tables[0].findAll('a', href=True))
    if not mirrors:
        raise Exception("Failed to discover any Debian mirrors! (using %s)" % MIRRORS_URL)
    logger.info("Discovered %s in %s.", pluralize(len(mirrors), "Debian mirror"), timer)
    return mirrors
def discover_mirrors ( ) : timer = Timer ( ) logger . info ( "Discovering Debian 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 = 20 , 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 mirrors = set ( ) if not tables : raise Exception ( "Failed to locate <table> element in Debian mirror page! (%s)" % MIRRORS_URL ) else : for row in tables [ 1 ] . findAll ( "tr" ) : # tables[1] organises mirrors by country. if flag : if not row . a : # End of mirrors located in that country break else : mirrors . add ( CandidateMirror ( mirror_url = row . a [ 'href' ] ) ) if row . get_text ( ) == country : flag = True if len ( mirrors ) < 3 : # Too few, add tables[0] which contains Primary Debian mirror sites all around the world. mirrors . add ( CandidateMirror ( mirror_url = a [ 'href' ] ) for a in tables [ 0 ] . findAll ( 'a' , href = True ) ) if not mirrors : raise Exception ( "Failed to discover any Debian mirrors! (using %s)" % MIRRORS_URL ) logger . info ( "Discovered %s in %s." , pluralize ( len ( mirrors ) , "Debian mirror" ) , timer ) return mirrors