将输出与列表/数组进行比较
我认为自己是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你的意思是:
?
You mean:
?
结果以“XX dns_entry”格式返回,因此您可以执行以下操作:
现在您可以与此列表进行比较。
The results are returned in the format 'XX dns_entry', so you can do:
Now you can compare against this list.
好吧,首先您必须从
x.to_text()
返回的内容中删除前导数字:您可以循环执行此操作,或者使用列表理解:
然后确保您期望的所有内容都在记录中
Alright, so first you have to drop the leading number from what
x.to_text()
returns:You can do that loopily, or with a list comprehension:
Then just make sure everything you expect is in the records
又怎样呢?
What about?
编辑:这似乎正是我所希望的,谢谢大家的帮助!
EDIT:This appears to do exactly what I was hoping for, thank you everyone for you help!