I'm trying to get informations about some hostnames, for example the country of the registrant. I've found IPWhois who should do the job, but apparently it doesn't give me the expected data.
For example, I would like to know to which country is related Nokia (answer : Finland). I know that their hostname is nokia.com. So I've tried this:
import socket
from ipwhois import IPWhois
from pprint import pprint
ip = socket.gethostbyname('nokia.com')
obj = IPWhois(ip)
pprint(obj.lookup_rdap(depth=1))
Sadly the resutls don't show any information about Nokia, but about the hosting, which is in the U.S.
Using the Whois in Ubuntu Network Tools, I can see :
[…]
Registrant Name: Nokia Corporation
Registrant Organization: Nokia Corporation
Registrant Street: P.O. Box 226, Nokia Group
Registrant City: Espoo
Registrant State/Province: Espoo
Registrant Postal Code: 00045
Registrant Country: FI
[…]
Is there a way to get this kind of data?
[Edit] The script should work on Ubuntu server (from 12.04). As suggested, I can use the subprocess library to call check_output, as for example:
from subprocess import check_output
output = check_output(['whois', 'nokia.com'])
Then I need to process the output, as it can be different from a registrar to another.