iptocountry (version 0.1, 04 August 2008)
index
iptocountry.py

Support library to work with the ip-to-country database.
 
Example use:
 
    # create obj
    ip2c = iptocountry(download_dir='.')
 
    # find addresses by 'dk' country
    dk_addr = ip2c.find_by_country('dk')
 
    # find the block of an address
    addr_block = ip2c.find_by_addr('195.140.132.34')

 
Modules
       
csv
logging
os
pickle
urllib
zipfile

 
Classes
       
addressblock
iptocountry

 
class addressblock
    Representation of an address block.
 
  Methods defined here:
__eq__(self, other)
if compared with a string, then assume string is an ip-address, and
check if this ip is in the addressblock that the object represents
__init__(self, start_dec, end_dec, cc_tld)
__len__(self)
Returns the length of the address block, ie. how many IP-addresses
it contains.
__str__(self)
ip_end(self)
Returns the last IP-address in the address-block.
ip_start(self)
Returns the first IP-address in the address-block.

 
class iptocountry
    An interface to work with the ip-to-country database.
It does loading of the database from the web, and caches it in a
pickle'd file.
 
After this, the database can be queried either by address,
or by country.
 
  Methods defined here:
__init__(self, download_dir=None)
Constructor, download_dir can be used to specify where to
cache the database from the web.
find_by_addr(self, ip_addr)
Find the block which the ip_addr IP-address belongs to. Returns
None if it is not found.
find_by_country(self, cc_tld)
Find all the address block which belong to a country code TLD.
cc_tld is the country code TLD, eg. 'dk' for Denmark, or 'us' for
USA.
initialize(self, new_url=None)
Downloads the database from the web, and process it into
a pickle'd file. new_url is an alternative URL where to load
the file from.

Data and other attributes defined here:
IP2C_CSV = 'ip-to-country.csv'
IP2C_URL = 'http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip'
PICKLE_FILE = 'ip-to-country.pkl'

 
Functions
       
dec_to_ip(n)
Converts a decimal number to a dotted ip.ip.ip.ip
ip_to_dec(ip)
Converts a dotted ip.ip.ip.ip into its decimal representation

 
Data
        __author__ = 'Soren Davidsen <soren@tanesha.net>'
__date__ = '04 August 2008'
__version__ = '0.1'

 
Author
        Soren Davidsen <soren@tanesha.net>