检查Python DNS解析器查询的特定输出

2 投票
1 回答
1256 浏览
提问于 2025-04-18 02:18

我正在尝试写一个程序,让用户输入一个特定的域名(比如说,Google.com)。如果 _spf.google.com 在 Google.com 的 SPF TXT 记录中,我想让它显示“是的”。如果没有,我想让它显示“不是”。现在,代码会让我输入域名,然后查找 SPF 记录,但我就是无法让它显示“是的”。这是为什么呢?我试过把它变成字符串,但即便如此也没能达到我想要的效果。我哪里出错了呢?

另外,你们觉得我应该从哪里开始,才能弄明白我需要写什么代码来计算一个 SPF 记录到底使用了多少次 DNS 查询呢?

    import dns.resolver


    question= raw_input("What domain do you want? ")

    def PrintandGoogle(question):

      answer=dns.resolver.query(question,"TXT")
      for data in answer:
        print data
        if "_spf.google.com" in answer:
          print "Yup."
        else:
          print "Nope."

    printAndGoogle(question)

1 个回答

2

如果你的 if 语句是在循环里面:

    if "_spf.google.com" in data.to_text():

如果你的 if 语句是在循环外面:

if any("_spf.google.com" in data.to_text() for data in answer):

撰写回答