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.

Querying coordinates of a single country:

 >>> whois.geomap.getLongLat('SPAIN')
 (40.0, -4.0)

Drawing the world map with circles at given countries:

 >>> from pylab import show
 >>> whois.geomap.world({'CANADA': 48, 'FRANCE': 4, 'INDIA': 570})
 >>> show()

Submodules
  • geomap: whois Python module ------------------- Geographical localisation of countries thanks to a longitude and latitude database.
  • requester: Internet whois client module.

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'
str __ModulePath__ = '/home/jm/dev/lib/whois'
str __url__ = 'http://philipjm.free.fr/blog/'
str __version__ = '0.2.0'
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'                                  

__ModulePath__

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

__url__

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

__version__

Type:
str
Value:
'0.2.0'                                                                

WhoisServer

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

Generated by Epydoc 2.1 on Sat Mar 31 22:33:24 2007 http://epydoc.sf.net