dnspython模块使用(解析域名)

import sys

import socket

import dns.resolver

print 'Argument List:', str(sys.argv)

site = sys.argv[1]

dns_server = sys.argv[2]

# Basic CNAME query the host's DNS

for rdata in dns.resolver.query(site, 'CNAME') :

print rdata.target

# Basic A query the host's DNS

for rdata in dns.resolver.query(site, 'A') :

print rdata.address

###下边是制定dns解析的服务器设置

# Setting an specific DNS Server

resolver = dns.resolver.Resolver()

resolver.nameservers = [socket.gethostbyname(dns_server)]

# Basic CNAME query with the specific DNS server

answer = resolver.query(site, 'CNAME');

for rdata in answer :

print rdata.target

# Basic A query with the specific DNS server

answer = resolver.query(site, 'A');

for rdata in answer :

print rdata.address

To run:(命令执行)

python dnsdig.py www.youtube.com 8.8.8.8

你可能感兴趣的:(dnspython模块使用(解析域名))