Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added verbose lookup & memcaching off by default #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions cymruwhois.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def make_key(self, arg):
else:
return "cymruwhois:ip:" + arg

def __init__(self, host="whois.cymru.com", port=43, memcache_host='localhost:11211'):
def __init__(self, host="whois.cymru.com", port=43, memcache_host=None):
self.host=host
self.port=port
self._connected=False
Expand Down Expand Up @@ -145,12 +145,14 @@ def read_and_discard(self):
finally:
self.socket.setblocking(1)

def _begin(self):
def _begin(self,verbose=False):
"""Explicitly connect and send BEGIN to start the lookup process"""
self._connect()
self._sendline("BEGIN")
self._readline() #discard the message "Bulk mode; one IP per line. [2005-08-02 18:54:55 GMT]"
self._sendline("PREFIX\nASNUMBER\nCOUNTRYCODE\nNOTRUNC")
if verbose:
self._sendline("VERBOSE")
self._connected=True

def disconnect(self):
Expand All @@ -174,16 +176,16 @@ def cache(self, r):
return
self.c.set(self.make_key(r.key), r, 60*60*6)

def lookup(self, ip):
def lookup(self, ip,verbose=False):
"""Look up a single address.

.. warning::
Do not call this function inside of a loop, the performance
will be terrible. Instead, call lookupmany or lookupmany_dict
"""
return list(self.lookupmany([ip]))[0]
return list(self.lookupmany([ip],verbose))[0]

def lookupmany(self, ips):
def lookupmany(self, ips,verbose=False):
"""Look up many ip addresses"""
ips = [str(ip).strip() for ip in ips]

Expand All @@ -192,22 +194,26 @@ def lookupmany(self, ips):
not_cached = [ip for ip in batch if not cached.get(ip)]
#print "cached:%d not_cached:%d" % (len(cached), len(not_cached))
if not_cached:
for rec in self._lookupmany_raw(not_cached):
for rec in self._lookupmany_raw(not_cached,verbose):
cached[rec.key] = rec
for ip in batch:
if verbose:
ip=ip.split()[0] # reduce IP
""" TODO: Implement better caching for verbose lookups
"""
if ip in cached:
yield cached[ip]

def lookupmany_dict(self, ips):
def lookupmany_dict(self, ips,verbose=False):
"""Look up many ip addresses, returning a dictionary of ip -> record"""
ips = set(ips)
return dict((r.key, r) for r in self.lookupmany(ips))
return dict((r.key, r) for r in self.lookupmany(ips,verbose))

def _lookupmany_raw(self, ips):
def _lookupmany_raw(self, ips,verbose=False):
"""Do a look up for some ips"""

if not self._connected:
self._begin()
self._begin(verbose)
ips = set(ips)
for ip in ips:
self._sendline(ip)
Expand All @@ -220,6 +226,8 @@ def _lookupmany_raw(self, ips):
need -=1
continue
parts=result.split("|")
if verbose:
parts=parts[0:4]+[parts[-1]]
if len(parts)==5:
r=record(*parts)
else:
Expand Down