Package whois
[frames | no frames]

Package whois

whois Python module
-------------------

Guess host location from their IP, based on whois services.

2 method are implemented:

* IP searched in a local database
* IP searched on an Internet whois service

Search results are stored in a local database to speedup further identical
searches. Use of former results can be disabled. Internet whois search can
be disabled too.

Local databases can be downloaded/updated from:
   http://software77.net/cgi-bin/ip-country/geo-ip.pl
   http://ip-to-country.webhosting.info/node/view/6

Full Internet whois queries may also be accessed.

Usage
-----

Import whois module:

 >>> import whois
 > loaded 79552 IP ranges from /usr/lib/.../whois/db/IpToCountry.csv.db

Query host country:

 >>> print whois.guessIPcountry('194.109.137.218')
 NETHERLANDS

Full Internet whois query:

 >>> Whois = whois.WhoisConsumer('194.109.137.218')
 >>> whois.WhoisRequest(Whois, whois.WhoisServer)
 <whois._whois.WhoisRequest at -0x4a62ce74>
 >>> whois.asyncore.loop()
 >>> print Whois.text
 
 OrgName:    RIPE Network Coordination Centre
 OrgID:      RIPE
 Address:    P.O. Box 10096
 City:       Amsterdam
 StateProv:
 PostalCode: 1001EB
 Country:    NL
 
 ReferralServer: whois://whois.ripe.net:43
 
 NetRange:   194.0.0.0 - 194.255.255.255
 CIDR:       194.0.0.0/8
 NetName:    RIPE-CBLK2
 NetHandle:  NET-194-0-0-0-1
 Parent:
 NetType:    Allocated to RIPE NCC
 NameServer: NS-PRI.RIPE.NET
 NameServer: NS3.NIC.FR
 NameServer: SUNIC.SUNET.SE
 NameServer: NS-EXT.ISC.ORG
 NameServer: SEC1.APNIC.NET
 NameServer: SEC3.APNIC.NET
 NameServer: TINNIE.ARIN.NET
 Comment:    These addresses have been further assigned to users in
 Comment:    the RIPE NCC region. Contact information can be found in
 Comment:    the RIPE database at http://www.ripe.net/whois
 RegDate:    1993-07-21
 Updated:    2005-08-03
 
 # ARIN WHOIS database, last updated 2007-03-24 19:10
 # Enter ? for additional hints on searching ARIN's WHOIS database.

Submodules

Function Summary
  addDatabase(IP, CountryName)
Records a searched IP into searched IP database.
  guessIPcountry(IP, noInternet, recompute)
Guesses the IP country using local database and eventually an Internet whois service.
  IP2num(IP)
Converts a string IP (xx.xx.xx.xx) into an integer number.
  loadDatabase()
Loads the database of previously searched IP.
  num2IP(IPnum)
Converts a string IP (xx.xx.xx.xx) into an integer number.
  readIPCountryFile(verbose)
Reads an IP/country database file.
  saveDatabase()
Saves the searched IP database.

Variable Summary
str __author__ = 'jean-michel.philippe@libertysurf.fr'
dict __DB__ = {3389112322L: 'MALDIVES', 3363168260L: 'ARGENTI...
str __DBfile__ = '/home/jm/dev/lib/whois/db/searched-IPs.db'...
str __IPCountryFile__ = '/home/jm/dev/lib/whois/db/IpToCount...
str __ModulePath__ = '/home/jm/dev/lib/whois'
str __url__ = 'http://philipjm.free.fr/blog/'
str __version__ = '0.1.0'
ndarray FromIPList = array([         0,   50331648,   67108864, ...
list IPCountryList = ['RESERVED', 'UNITED STATES', 'UNITED ST...
dict IPCountryTable = {'BD': 'BANGLADESH', 'BE': 'BELGIUM', '...
ndarray ToIPList = array([  16777215,   67108863,   83886079, .....
str WhoisServer = 'whois.arin.net'

Function Details

addDatabase(IP, CountryName)

Records a searched IP into searched IP database.
>>> addDatabase(IP, CountryName)
Input:
  • IP = integer number as returned by IP2num
  • CountryName = full country name

guessIPcountry(IP, noInternet=False, recompute='none')

Guesses the IP country using local database and eventually an Internet whois service. Local database is always queried before any Internet service. Internet services are queried if local database cannot answer the query.
>>> CountryName = guessIPcountry(IP, noInternet=False, recompute='none')
Input:
  • IP = IP string
  • noInternet = never use Internet whois service
  • recompute = 'none', 'all' or 'unknown'
Output:
  • CountryName = full country name, '?' if not found in databases
Meaning of the recompute values:
  • 'none' = never search IP in whois databases if the IP was previously searched (searched IP database)
  • 'all' = always search IP in whois databases
  • 'unknown' = as 'none' but IP that was previously unknown in databases (CountryName = '?') are searched again

IP2num(IP)

Converts a string IP (xx.xx.xx.xx) into an integer number.
>>> IPnum = IP2num(IP)
Input:
  • IP = IP string
Output:
  • IPnum = IP equivalent integer number

loadDatabase()

Loads the database of previously searched IP.
>>> loadDatabase()

num2IP(IPnum)

Converts a string IP (xx.xx.xx.xx) into an integer number.
>>> IP = num2IP(IPnum)
Input:
  • IPnum = IP equivalent integer number
Output:
  • IP = IP string

readIPCountryFile(verbose=True)

Reads an IP/country database file.
>>> readIPCountryFile(verbose=True)
Input:
  • verbose = display some information

saveDatabase()

Saves the searched IP database.
>>> Data = saveDatabase()
Output:
  • Data = dictionary Data[IP] = country
NB: IP is an integer number as returned by IP2num

Variable Details

__author__

Type:
str
Value:
'jean-michel.philippe@libertysurf.fr'                                  

__DB__

Type:
dict
Value:
{1074642955: 'UNITED STATES',
 1074642956: 'UNITED STATES',
 1074642957: 'UNITED STATES',
 3260030985L: 'UNITED KINGDOM',
 3260030986L: 'UNITED KINGDOM',
 3363168260L: 'ARGENTINA',
 3367084037L: 'BRAZIL',
 3389112322L: 'MALDIVES',
...                                                                    

__DBfile__

Type:
str
Value:
'/home/jm/dev/lib/whois/db/searched-IPs.db'                            

__IPCountryFile__

Type:
str
Value:
'/home/jm/dev/lib/whois/db/IpToCountry.csv'                            

__ModulePath__

Type:
str
Value:
'/home/jm/dev/lib/whois'                                               

__url__

Type:
str
Value:
'http://philipjm.free.fr/blog/'                                        

__version__

Type:
str
Value:
'0.1.0'                                                                

FromIPList

Type:
ndarray
Value:
array([         0,   50331648,   67108864, ..., 4244635648, 4261412864\
,
       4278190080], dtype=int64)                                       

IPCountryList

Type:
list
Value:
['RESERVED',
 'UNITED STATES',
 'UNITED STATES',
 'UNITED STATES',
 'UNITED STATES',
 'UNITED STATES',
 'UNITED STATES',
 'RESERVED',
...                                                                    

IPCountryTable

Type:
dict
Value:
{'BA': 'BOSNIA AND HERZEGOWINA',
 'BB': 'BARBADOS',
 'BD': 'BANGLADESH',
 'BE': 'BELGIUM',
 'BF': 'BURKINA FASO',
 'BG': 'BULGARIA',
 'BM': 'BERMUDA',
 'BN': 'BRUNEI DARUSSALAM',
...                                                                    

ToIPList

Type:
ndarray
Value:
array([  16777215,   67108863,   83886079, ..., 4261412863, 4278190079\
,
       4294967295], dtype=int64)                                       

WhoisServer

Type:
str
Value:
'whois.arin.net'                                                       

Generated by Epydoc 2.1 on Mon Mar 26 22:50:05 2007 http://epydoc.sf.net