DNS(Domain Name System,域名系统)是互联网的一项核心服务,它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网。DNS域名解析方式主要有递归查询和迭代查询两种。
import dns.resolver
def resolve_domain(domain):
try:
answers = dns.resolver.resolve(domain, 'A')
for rdata in answers:
print(f'{domain} resolves to {rdata}')
except dns.resolver.NXDOMAIN:
print(f'The domain {domain} does not exist.')
except dns.resolver.NoAnswer:
print(f'The domain {domain} has no A records.')
except dns.resolver.Timeout:
print(f'The query for {domain} timed out.')
except Exception as e:
print(f'An error occurred: {e}')
resolve_domain('example.com')
这段代码使用了Python的dnspython
库来执行DNS查询,并处理了可能出现的几种异常情况。
领取专属 10元无门槛券
手把手带您无忧上云