这个名字是什么记录
按记录表,shop.example.com 登记的最主要是什么类型?
贯穿本节的记录表 RECORDS,每条是(名字, 类型, 值, 优先级):A=域名对 IPv4、AAAA=对 IPv6、CNAME=别名指向另一个名字、MX=收信服务器(带优先级,数字小的先用)。type_of 交回一个名字最主要的记录类型,resolve 跟着 CNAME 链一路走到 A 记录的地址,mx_top 挑优先级最小的收信服务器。
RECORDS = [
("www.example.com", "A", "93.184.216.34", 0),
("www.example.com", "AAAA", "2606:2800:220:1:248:1893:25c8:1946", 0),
("example.com", "MX", "mail.example.com", 10),
("example.com", "MX", "backup.example.com", 20),
("shop.example.com", "CNAME", "www.example.com", 0),
("mail.example.com", "A", "93.184.216.35", 0),
("backup.example.com", "A", "93.184.216.36", 0),
]
def records_of(name):
return [r for r in RECORDS if r[0] == name]
def type_of(name):
# 这个名字最主要的记录类型(一个名字可能有多条,取第一条登记的)
for n, t, v, p in RECORDS:
if n == name:
return t
return "无"
def resolve(name):
# 跟着 CNAME 链一路走到 A 记录的地址(别名指向别名也跟)
seen = 0
while seen < 16:
seen += 1
hit = None
for n, t, v, p in RECORDS:
if n == name and t == "CNAME":
name = v
hit = "cname"
break
if hit:
continue
for n, t, v, p in RECORDS:
if n == name and t == "A":
return v
return "无"
return "无"
def mx_top(domain):
# 收信优先挑 MX 优先级数字最小的那台
mxs = sorted((p, v) for n, t, v, p in RECORDS if n == domain and t == "MX")
return mxs[0][1] if mxs else "无"
print(type_of("shop.example.com"))
全部评论