Think before you speak, read before you think.

python中DNS查询和反向查询

用到的还是python的socket库,用到函数getaddrinfo()

定义如下:

getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0)
例:

#!/usr/bin/env python
import sys, socket
result = socket.getaddrinfo("jpuyy.com", 80, 0, 0, socket.SOL_TCP)
print result

返回结果是

[(10, 1, 6, ”, (‘2604:6600:1059::5810:3a0a’, 80, 0, 0)), (2, 1, 6, ”, (‘216.24.201.107’, 80))]

执行反向查询

gethostbyaddr(ip_address)

查询主机名的详细信息

getfqdn([name])   #Return a fully qualified domain name for name.

http://docs.python.org/library/socket.html

在debian下面安装PyDNS( python-dns )进行DNS的高级查询

apt-get install python-dns

程序中,import DNS,先进行 DNS.DiscoverNameServers(),然后建立一个请求对象 DNS.Request()。请求后PyDNS会返回包含结果的对象。


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *