Python网络编程——DNS 查找
域名系统又称DNS ,是互联网的电话簿,与域名有关。 DNS 将域名转换为相应的 IP 地址,以便浏览器可以访问资源。 Python提供了 DNS 模块,用于处理域名到 IP 地址的这种转换。
查找记录
dnspython 模块提供 dns.resolver() 帮助查找域名的各种记录。该函数采用两个重要参数,域名和记录类型。下面列出了一些带有示例的记录类型:
- A Record:它是DNS记录的基本类型,这里A代表地址。它显示域的 IP 地址。
Python3
# Import libraries
import dns.resolver
# Finding A record
result = dns.resolver.query('geeksforgeeks.org', 'A')
# Printing record
for val in result:
print('A Record : ', val.to_text())
Python3
# Import libraries
import dns.resolver
# Finding AAAA record
result = dns.resolver.query('geeksforgeeks.org', 'AAAA')
# Printing record
for val in result:
print('AAAA Record : ', ipval.to_text())
Python3
# Import libraries
import dns.resolver
# Finding PTR record
result = dns.resolver.query('116.62.218.34.in-addr.arpa', 'PTR')
# Printing record
for val in result:
print('PTR Record : ', val.to_text())
Python3
# Import libraries
import dns.resolver
# Finding NS record
result = dns.resolver.query('geeksforgeeks.org', 'NS')
# Printing record
for val in result:
print('NS Record : ', val.to_text())
Python3
# Import libraries
import dns.resolver
# Finding MX record
result = dns.resolver.query('geeksforgeeks.org', 'MX')
# Printing record
for val in result:
print('MX Record : ', val.to_text())
Python3
# Import libraries
import dns.resolver
# Finding SOA record
result = dns.resolver.query('geeksforgeeks.org', 'SOA')
# Printing record
for val in result:
print('SOA Record : ', val.to_text())
Python3
# Import libraries
import dns.resolver
# Finding CNAME record
result = dns.resolver.query('geeksforgeeks.org', 'CNAME')
# Printing record
for val in result:
print('CNAME Record : ', val.target)
Python3
# Import libraries
import dns.resolver
# Finding TXT record
result = dns.resolver.query('geeksforgeeks.org', 'TXT')
# Printing record
for val in result:
print('TXT Record : ', val.to_text())
输出:
A Record : 34.218.62.116
- AAAA 记录:这是一个 IP 地址记录,用于查找连接到域的计算机的 IP。它在概念上类似于 A 记录,但仅指定服务器的 IPv6 地址而不是 IPv4。
蟒蛇3
# Import libraries
import dns.resolver
# Finding AAAA record
result = dns.resolver.query('geeksforgeeks.org', 'AAAA')
# Printing record
for val in result:
print('AAAA Record : ', ipval.to_text())
输出:
NoAnswer: The DNS response does not contain an answer to the question: geeksforgeeks.org. IN AAAA
- PTR Record: PTR代表指针记录,用于将IP地址转换为域名或主机名。它用于反向 DNS 查找。
蟒蛇3
# Import libraries
import dns.resolver
# Finding PTR record
result = dns.resolver.query('116.62.218.34.in-addr.arpa', 'PTR')
# Printing record
for val in result:
print('PTR Record : ', val.to_text())
输出:
PTR Record : ec2-34-218-62-116.us-west-2.compute.amazonaws.com.
- NS 记录:名称服务器 (NS) 记录提供了关于给定域的权威服务器的信息,即哪个服务器具有实际的 DNS 记录。一个域可以有多个 NS 记录,包括主名称服务器和备用名称服务器。
蟒蛇3
# Import libraries
import dns.resolver
# Finding NS record
result = dns.resolver.query('geeksforgeeks.org', 'NS')
# Printing record
for val in result:
print('NS Record : ', val.to_text())
输出:
NS Record : ns-1520.awsdns-62.org.
NS Record : ns-1569.awsdns-04.co.uk.
NS Record : ns-245.awsdns-30.com.
NS Record : ns-869.awsdns-44.net.
- MX Records: MX代表Mail Exchanger record,是一种资源记录,指定负责代表域接收邮件的邮件服务器。如果存在多个邮件服务器用于负载平衡和冗余,则它根据优先邮件具有优先级值。
蟒蛇3
# Import libraries
import dns.resolver
# Finding MX record
result = dns.resolver.query('geeksforgeeks.org', 'MX')
# Printing record
for val in result:
print('MX Record : ', val.to_text())
输出:
MX Record : 1 aspmx.l.google.com.
MX Record : 10 alt3.aspmx.l.google.com.
MX Record : 10 alt4.aspmx.l.google.com.
MX Record : 5 alt1.aspmx.l.google.com.
MX Record : 5 alt2.aspmx.l.google.com.
- SOA 记录: SOA 代表权限开始记录,这是一种资源记录类型,包含有关区域管理的信息,尤其是与区域管理员定义的区域传输相关的信息。
蟒蛇3
# Import libraries
import dns.resolver
# Finding SOA record
result = dns.resolver.query('geeksforgeeks.org', 'SOA')
# Printing record
for val in result:
print('SOA Record : ', val.to_text())
输出:
SOA Record : ns-869.awsdns-44.net. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400
- CNAME Record: CNAME 代表 Canonical Name record,用于将域名映射为其他域的别名。它总是指向另一个域,从不直接指向一个 IP。
蟒蛇3
# Import libraries
import dns.resolver
# Finding CNAME record
result = dns.resolver.query('geeksforgeeks.org', 'CNAME')
# Printing record
for val in result:
print('CNAME Record : ', val.target)
输出:
NoAnswer: The DNS response does not contain an answer to the question: geeksforgeeks.org. IN CNAME
- TXT 记录:这些记录包含域外来源的文本信息。 TXT 记录可用于各种目的,例如谷歌使用它们来验证域所有权并确保电子邮件安全。
蟒蛇3
# Import libraries
import dns.resolver
# Finding TXT record
result = dns.resolver.query('geeksforgeeks.org', 'TXT')
# Printing record
for val in result:
print('TXT Record : ', val.to_text())
输出:
TXT Record : “fob1m1abcdp777bf2ncvnjm08n”
TXT Record : “v=spf1 include:amazonses.com include:_spf.google.com ip4:167.89.66.115 -all”