Python program to fetch domain whois data using...

本文介绍了一个使用Python编写的简单程序,用于从域名获取WHOIS信息。WHOIS信息包括域名注册商、所有者、注册日期等详细信息。该程序通过连接WHOIS服务器并发送请求来获取这些数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自 http://www.binarytides.com/python-program-to-fetch-domain-whois-data-using-sockets/

Whois


The whois information of a domain name provides various details like registrar, owner, registration date, expiry date etc. The whois information is provided by the corresponding whois servers of the registrars. The first step is to contact whois.iana.org which provides the actual whois server of a domain name. Next the particular whois server is contacted which provides the full whois data of the domain.

Implementation is quite simple and python makes it even simpler.

#!/usr/bin/python

'''
Program to fetch whois information of a domain name
Silver Moon
m00n.silv3r@gmail.com
'''
import socket, sys

#Perform a generic whois query to a server and get the reply
def perform_whois(server , query) :
	#socket connection
	s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
	s.connect((server , 43))
	
	#send data
	s.send(query + '\r\n')
	
	#receive reply
	msg = ''
	while len(msg) < 10000:
		chunk = s.recv(100)
		if(chunk == ''):
			break
		msg = msg + chunk
	
	return msg
#End

#Function to perform the whois on a domain name
def get_whois_data(domain):
	
	#remove http and www
	domain = domain.replace('http://','')
	domain = domain.replace('www.','')
	
	#get the extension , .com , .org , .edu
	ext = domain[-3:]
	
	#If top level domain .com .org .net
	if(ext == 'com' or ext == 'org' or ext == 'net'):
		whois = 'whois.internic.net'
		msg = perform_whois(whois , domain)
		
		#Now scan the reply for the whois server
		lines = msg.splitlines()
		for line in lines:
			if ':' in line:
				words = line.split(':')
				if  'Whois' in words[0] and 'whois.' in words[1]:
					whois = words[1].strip()
					break;
	
	#Or Country level - contact whois.iana.org to find the whois server of a particular TLD
	else:
		#Break again like , co.uk to uk
		ext = domain.split('.')[-1]
		
		#This will tell the whois server for the particular country
		whois = 'whois.iana.org'
		msg = perform_whois(whois , ext)
		
		#Now search the reply for a whois server
		lines = msg.splitlines()
		for line in lines:
			if ':' in line:
				words = line.split(':')
				if 'whois.' in words[1] and 'Whois Server (port 43)' in words[0]:
					whois = words[1].strip()
					break;
	
	#Now contact the final whois server
	msg = perform_whois(whois , domain)
	
	#Return the reply
	return msg
# end
        
# get the domain name from command line argument
domain_name = sys.argv[1]
print get_whois_data(domain_name)

Run the program by issuing the following command at the terminal.

$ python whois.py stackoverflow.com

The commandline argument should contain the domain name. The output would be the whois data.

转载于:https://my.oschina.net/yisenn/blog/85056

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值