如何使用PHP或Python代码获取有关域名的信息(例如所有者或服务器详细信息)?我想避免使用任何第三方网站。
这可能吗?
答案 0 :(得分:2)
您可以基于以下whois脚本:http://www.phpeasycode.com/whois/
该脚本首先检查正确的whois服务器,然后在端口43上打开一个套接字。这是一个基于上面演示代码的简化查询功能。
每个TLD都有自己的whois服务器。您可以在此处找到完整列表:http://www.iana.org/domains/root/db/和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;
答案 1 :(得分:1)
首先让您的生活更轻松:
pip install python-whois
pip install requests
然后执行以下操作:
>>> 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: ['e89d6901ba3e470e8cedc3eaa32a0074-1697561@contact.gandi.net', 'e89d6901ba3e470e8cedc3eaa32a0074-1697561@contact.gandi.net', 'infrastructure-staff@python.org']
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:
>>>
答案 2 :(得分:0)
实际上,以前的答案现在是错误的,urlparse
更改为urllib.parse
,所以它将是:
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))