WHOIS协议是一个非常简单的Internet信息查询协议;在RFC812文档中有定义,先向服务器的TCP 43端口建立一个连接,发送要查询的域名关键字并以回车换行结尾,然后接收服务器返回信息,服务器输出完毕后会立即断开连接。
RFC812文档:http://www.faqs.org/rfcs/rfc812.html
Python实现:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('whois.internic.net', 43))
s.send(b"baidu.com \r\n")
response =b''
while True:
data = s.recv(4096)
response += data
if not data :
break
s.close()
print (response.decode())
Golang 实现:
package main
import (
"io"
"fmt"
"net"
)
func main() {
conn,_ := net.Dial("tcp","whois.internic.net:43")
conn.Write([]byte("baidu.com \r\n"))
buf := make([]byte,4096)
n,err := conn.Read(buf)
if err != nil && err != io.EOF {
fmt.Println(err)
}
fmt.Println(string(buf[:n]))
conn.Close()
}
返回的部分内容:
Domain Name: BAIDU.COM Registry Domain ID: 11181110_DOMAIN_COM-VRSN Registrar WHOIS Server: whois.markmonitor.com Registrar URL: http://www.markmonitor.com Updated Date: 2019-05-09T04:30:46Z Creation Date: 1999-10-11T11:05:17Z Registry Expiry Date: 2026-10-11T11:05:17Z Registrar: MarkMonitor Inc. Registrar IANA ID: 292 Registrar Abuse Contact Email: abusecomplaints@markmonitor.com Registrar Abuse Contact Phone: +1.2083895740 Domain Status: clientDeleteProhibited https://icann.org/epp#clientDeleteProhibited Domain Status: clientTransferProhibited https://icann.org/epp#clientTransferProhibited Domain Status: clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited Domain Status: serverDeleteProhibited https://icann.org/epp#serverDeleteProhibited Domain Status: serverTransferProhibited https://icann.org/epp#serverTransferProhibited Domain Status: serverUpdateProhibited https://icann.org/epp#serverUpdateProhibited Name Server: NS1.BAIDU.COM Name Server: NS2.BAIDU.COM Name Server: NS3.BAIDU.COM Name Server: NS4.BAIDU.COM Name Server: NS7.BAIDU.COM DNSSEC: unsigned URL of the ICANN Whois Inaccuracy Complaint Form: https://www.icann.org/wicf/