简体   繁体   English

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

[英]Comparing output to a list/array

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. 我认为自己是python(和一般来说编程!)的初学者,但是我正在通过Zed A Shaw的“以艰难的方式学习python”的工作,并且正在慢慢地整理东西。 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: 我正在编写一个小脚本,以检查域的实时mx记录是否符合预期且尚未更改(长话),到目前为止,我具有以下内容:

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. 这使用dnspython模块吐出邮件主机和首选项编号。 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. 我现在需要做的是将此输出与两个预期结果进行比较,因此对于bbc.co.uk,这些将是cluster1a.eu.messagelabs.com。 & cluster1.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!"

You mean: 你的意思是:

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

?

The results are returned in the format 'XX dns_entry', so you can do: 结果以“ 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.']

Now you can compare against this list. 现在,您可以与此列表进行比较。

Alright, so first you have to drop the leading number from what x.to_text() returns: 好吧,所以首先您必须删除x.to_text()返回值的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.

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.

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))

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!"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM