'The function for retrieving whois or rwhois information for an IP
address via any port. Defaults to port 43/tcp (WHOIS).
Args:
asn_registry: The NIC to run the query against.
retry_count: The number of times to retry in case socket errors,
timeouts, connection resets, etc. are encountered.
server: An optional server to connect to. If provided, asn_registry
will be ignored.
port: The network port to connect on.
extra_blacklist: A list of blacklisted whois servers in addition to
the global BLACKLIST.
Returns:
String: The raw whois data.
Raises:
BlacklistError: Raised if the whois server provided is in the
global BLACKLIST or extra_blacklist.
WhoisLookupError: The whois lookup failed.'

def get_whois(self, asn_registry='arin', retry_count=3, server=None, port=43, extra_blacklist=None):

    try:
        extra_bl = (extra_blacklist if extra_blacklist else [])
        if any(((server in srv) for srv in (BLACKLIST, extra_bl))):
            raise BlacklistError('The    server    {0}    is    blacklisted.'.format(server))
        if (server is None):
            server = RIR_WHOIS[asn_registry]['server']
        conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        conn.settimeout(self.timeout)
        log.debug('WHOIS    query    for    {0}    at    {1}:{2}'.format(self.address_str, server, port))
        conn.connect((server, port))
        query = (self.address_str + '\r\n')
        if (asn_registry == 'arin'):
            query = 'n    +    {0}'.format(query)
        conn.send(query.encode())
        response = ''
        while True:
            d = conn.recv(4096).decode('ascii', 'ignore')
            response += d
            if (not d):
                break
        conn.close()
        if ('Query    rate    limit    exceeded' in response):
            log.debug('WHOIS    query    rate    limit    exceeded.    Waiting...')
            sleep(1)
            return self.get_whois(asn_registry=asn_registry, retry_count=(retry_count - 1), server=server, port=port, extra_blacklist=extra_blacklist)
        elif (('error    501' in response) or ('error    230' in response)):
            log.debug('WHOIS    query    error:    {0}'.format(response))
            raise ValueError
        return str(response)
    except (socket.timeout, socket.error) as e:
        log.debug('WHOIS    query    socket    error:    {0}'.format(e))
        if (retry_count > 0):
            log.debug('WHOIS    query    retrying    (count:    {0})'.format(str(retry_count)))
            return self.get_whois(asn_registry=asn_registry, retry_count=(retry_count - 1), server=server, port=port, extra_blacklist=extra_blacklist)
        else:
            raise WhoisLookupError('WHOIS    lookup    failed    for    {0}.'.format(self.address_str))
    except BlacklistError:
        raise
    except:
        raise WhoisLookupError('WHOIS    lookup    failed    for    {0}.'.format(self.address_str))
