How can I get information about a domain name (such as owner or server details) using PHP or Python code? I'd like to avoid using any 3rd party web site.
Is this possible?
How can I get information about a domain name (such as owner or server details) using PHP or Python code? I'd like to avoid using any 3rd party web site.
Is this possible?
You can base yourself on the following whois script: http://www.phpeasycode.com/whois/
The script first checks for the right whois server and then opens a socket on port 43. Here's a simpliefied query function based on the code from the demo above.
Each TLD has its own whois server. You can find a complete list here : http://www.iana.org/domains/root/db/ and http://www.whois365.com/en/listtld/
<?php
$whoisserver = "whois.pir.org";
$domain = "example.org";
$port = 43;
$timeout = 10;
$fp = @fsockopen($whoisserver, $port, $errno, $errstr, $timeout) or die("Socket Error " . $errno . " - " . $errstr);
fputs($fp, $domain . "\r\n");
$out = "";
while(!feof($fp)){
$out .= fgets($fp);
}
fclose($fp);
$res = "";
if((strpos(strtolower($out), "error") === FALSE) && (strpos(strtolower($out), "not allocated") === FALSE)) {
$rows = explode("\n", $out);
foreach($rows as $row) {
$row = trim($row);
if(($row != '') && ($row{0} != '#') && ($row{0} != '%')) {
$res .= $row."\n";
}
}
}
print $res;
First make your live easier:
pip install python-whois
pip install requests
Then do something like:
>>> import requests
>>> import urlparse
>>> import whois
>>> url = 'http://docs.python.org/3/'
>>> requests.head(url).headers['server']
'Apache/2.2.22 (Debian)'
>>> hostname = urlparse.urlparse(url).netloc
>>> print whois.whois(hostname)
creation_date: 1995-03-27 05:00:00
domain_name: PYTHON.ORG
emails: ['[email protected]', '[email protected]', '[email protected]']
expiration_date: []
name_servers: ['NS3.P11.DYNECT.NET', 'NS1.P11.DYNECT.NET', 'NS2.P11.DYNECT.NET', 'NS4.P11.DYNECT.NET', '', '', '', '', '', '', '', '', '']
referral_url:
registrar: Gandi SAS (R42-LROR)
status: clientTransferProhibited
updated_date: 2013-08-15 00:20:19
whois_server:
>>>
actually, the previus answer now is wrong, urlparse change to urllib.parse, so it will be :
import requests
import urllib.parse
import whois
url = 'http://docs.python.org/3/'
requests.head(url).headers['server']
'Apache/2.2.22 (Debian)'
hostname = urllib.parse.urlparse(url).netloc
print (whois.whois(hostname))