'The function for retrieving ASN information for an IP address from
Cymru via port 43/tcp (WHOIS).
Args:
retry_count: The number of times to retry in case socket errors,
timeouts, connection resets, etc. are encountered.
result: Optional result object. This bypasses the ASN lookup.
Returns:
Dictionary: A dictionary containing the following keys:
asn (String) - The Autonomous System Number.
asn_date (String) - The ASN Allocation date.
asn_registry (String) - The assigned ASN registry.
asn_cidr (String) - The assigned ASN CIDR.
asn_country_code (String) - The assigned ASN country code.
Raises:
ASNRegistryError: The ASN registry is not known.
ASNLookupError: The ASN lookup failed.'

def get_asn_whois(self, retry_count=3, result=None):

    try:
        if (result is None):
            conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            conn.settimeout(self.timeout)
            log.debug('ASN    query    for    {0}'.format(self.address_str))
            conn.connect((CYMRU_WHOIS, 43))
            conn.send('    -r    -a    -c    -p    -f    -o    {0}{1}'.format(self.address_str, '\r\n').encode())
            data = ''
            while True:
                d = conn.recv(4096).decode()
                data += d
                if (not d):
                    break
            conn.close()
        else:
            data = result
        temp = str(data).split('|')
        ret = {'asn_registry': temp[4].strip('    \n')}
        if (ret['asn_registry'] not in RIR_WHOIS.keys()):
            raise ASNRegistryError('ASN    registry    {0}    is    not    known.'.format(ret['asn_registry']))
        ret['asn'] = temp[0].strip('    \n')
        ret['asn_cidr'] = temp[2].strip('    \n')
        ret['asn_country_code'] = temp[3].strip('    \n').upper()
        ret['asn_date'] = temp[5].strip('    \n')
        return ret
    except (socket.timeout, socket.error) as e:
        log.debug('ASN    query    socket    error:    {0}'.format(e))
        if (retry_count > 0):
            log.debug('ASN    query    retrying    (count:    {0})'.format(str(retry_count)))
            return self.get_asn_whois((retry_count - 1))
        else:
            raise ASNLookupError('ASN    lookup    failed    for    {0}.'.format(self.address_str))
    except ASNRegistryError:
        raise
    except:
        raise ASNLookupError('ASN    lookup    failed    for    {0}.'.format(self.address_str))
