将输出与列表/数组进行比较

发布于 2024-10-05 16:36:08 字数 1095 浏览 0 评论 0原文

我认为自己是 python 的初学者(以及一般的编程!),但我正在努力学习 Zed A Shaw 的“艰难地学习 python”,并放慢了学习的速度。 我正在编写一个小脚本来检查域的实时 mx 记录是否符合预期并且没有被更改(长话短说),到目前为止我有以下内容:

import dns.resolver
domain = 'bbc.co.uk'
for x in dns.resolver.query(domain,'MX',):
    print x.to_text()

这使用 dnspython 模块吐出邮件主机并偏好号码。我现在需要做的是将这个输出与两个预期结果进行比较,因此对于 bbc.co.uk 来说,这些结果将是 cluster1a.eu.messagelabs.com。 & cluster1.eu.messagelabs.com。 (它们的顺序根据当前的首选项编号而变化)

我认为最好的方法是将预期结果添加到数组/列表中,并让脚本尝试将输出与数组/列表进行比较并提供真实的结果或错误的陈述,但在花了一整天尝试不同的代码安排之后,事实证明到目前为止这超出了我的理解范围。

最终,如果结果错误,我希望它提醒自己或我的同事,但这可以等到以后,因为我还没有决定实施此操作的最佳方法。 有哪位好心人能给我一个粗略的轮廓,说明实现我希望的结果的最佳实践是什么?

我感谢任何人花时间阅读本文:)

谢谢你,克里斯

编辑:这似乎正是我所希望的,谢谢大家的帮助!

import dns.resolver
domain = 'bbc.co.uk'
expected_responses = ['cluster1.eu.messagelabs.com.', 'cluster1a.eu.messagelabs.com.']
for x in dns.resolver.query(domain, 'MX'):
        if x.to_text().split()[1] not in expected_responses:
                print "Unexpected MX record found!"
        else:
                print x.to_text().split()[1] + " OK!"

I consider myself a very beginner at python(and programming in general!), but I am working though "learn python the hard way" by Zed A Shaw and slowing picking things up.
I'm writing a little script to check if the live mx records of a domain are to be as expected and have not been changed (long story) and so far I have the following:

import dns.resolver
domain = 'bbc.co.uk'
for x in dns.resolver.query(domain,'MX',):
    print x.to_text()

This uses the dnspython module to spit out the mailhost and the preference number. What I need to do now is compare this output to the two expected results, so for bbc.co.uk those would be cluster1a.eu.messagelabs.com. & cluster1.eu.messagelabs.com. (Their ordering changes depending on the current preference number)

I thought the best way to do this would to be to add the expected results to a array/list and have the script try and compare the output to the array/list and provide a true or false statement, but after spending all day trying different arrangements of code this is proving to be beyond my understanding so far.

Eventually I would like it to alert myself or my colleagues if the result come up false, but that can wait until later as I haven't decided on the best method for this to be implemented.
Would any kind soul be able to give me a rough outline of what the best practice would be to achieve the result I am hoping for?

I appreciate anyone taking the time to read this :)

Thank you, Chris

EDIT:This appears to do exactly what I was hoping for, thank you everyone for you help!

import dns.resolver
domain = 'bbc.co.uk'
expected_responses = ['cluster1.eu.messagelabs.com.', 'cluster1a.eu.messagelabs.com.']
for x in dns.resolver.query(domain, 'MX'):
        if x.to_text().split()[1] not in expected_responses:
                print "Unexpected MX record found!"
        else:
                print x.to_text().split()[1] + " OK!"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

流星番茄 2024-10-12 16:36:08

你的意思是:

x.to_text() in {'cluster1a.eu.messagelabs.com', 'cluster1.eu.messagelabs.com'}

You mean:

x.to_text() in {'cluster1a.eu.messagelabs.com', 'cluster1.eu.messagelabs.com'}

?

永不分离 2024-10-12 16:36:08

结果以“XX dns_entry”格式返回,因此您可以执行以下操作:

import dns.resolver
domain = 'bbc.co.uk'
results = []
for x in dns.resolver.query(domain,'MX',):
    results.append(x.to_text().split(' ')[1])
print results

>>> ['cluster1.eu.messagelabs.com.', 'cluster1a.eu.messagelabs.com.']

现在您可以与此列表进行比较。

The results are returned in the format 'XX dns_entry', so you can do:

import dns.resolver
domain = 'bbc.co.uk'
results = []
for x in dns.resolver.query(domain,'MX',):
    results.append(x.to_text().split(' ')[1])
print results

>>> ['cluster1.eu.messagelabs.com.', 'cluster1a.eu.messagelabs.com.']

Now you can compare against this list.

美煞众生 2024-10-12 16:36:08

好吧,首先您必须从 x.to_text() 返回的内容中删除前导数字:

txt = '20 cluster1a.eu.messagelabs.com.' # an example x.to_text()
txt = txt.split()[1] # Get rid of everything before (and including) the first space.

您可以循环执行此操作,或者使用列表理解:

records = [x.to_text().split()[1] for x in dns.resolver.query(domain, 'MX')]

然后确保您期望的所有内容都在记录中

expected = ['cluster1.eu.messagelabs.com.', 'cluster1a.eu.messagelabs.com.']
if False in [val in records for val in expected] or len(records) != len(expected):
    # Die.

Alright, so first you have to drop the leading number from what x.to_text() returns:

txt = '20 cluster1a.eu.messagelabs.com.' # an example x.to_text()
txt = txt.split()[1] # Get rid of everything before (and including) the first space.

You can do that loopily, or with a list comprehension:

records = [x.to_text().split()[1] for x in dns.resolver.query(domain, 'MX')]

Then just make sure everything you expect is in the records

expected = ['cluster1.eu.messagelabs.com.', 'cluster1a.eu.messagelabs.com.']
if False in [val in records for val in expected] or len(records) != len(expected):
    # Die.
幻想少年梦 2024-10-12 16:36:08

又怎样呢?

import dns.resolver

expected_domains = set(['cluster1a.eu.messagelabs.com.', 'cluster1.eu.messagelabs.com.'])
domains = set(str(mx.exchange) for mx in dns.resolver.query('bbc.co.uk', 'MX'))
if not domains.issuperset(expected_domains):
    print("Missing MX domains:", ", ".join(expected_domains - domains))

What about?

import dns.resolver

expected_domains = set(['cluster1a.eu.messagelabs.com.', 'cluster1.eu.messagelabs.com.'])
domains = set(str(mx.exchange) for mx in dns.resolver.query('bbc.co.uk', 'MX'))
if not domains.issuperset(expected_domains):
    print("Missing MX domains:", ", ".join(expected_domains - domains))
水染的天色ゝ 2024-10-12 16:36:08

编辑:这似乎正是我所希望的,谢谢大家的帮助!

import dns.resolver
domain = 'bbc.co.uk'
expected_responses = ['cluster1.eu.messagelabs.com.', 'cluster1a.eu.messagelabs.com.']
for x in dns.resolver.query(domain, 'MX'):
        if x.to_text().split()[1] not in expected_responses:
                print "Unexpected MX record found!"
        else:
                print x.to_text().split()[1] + " OK!"

EDIT:This appears to do exactly what I was hoping for, thank you everyone for you help!

import dns.resolver
domain = 'bbc.co.uk'
expected_responses = ['cluster1.eu.messagelabs.com.', 'cluster1a.eu.messagelabs.com.']
for x in dns.resolver.query(domain, 'MX'):
        if x.to_text().split()[1] not in expected_responses:
                print "Unexpected MX record found!"
        else:
                print x.to_text().split()[1] + " OK!"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文