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. 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 = ''
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 those would be & (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 = ''
expected_responses = ['', '']
for x in dns.resolver.query(domain, 'MX'):
        if x.to_text().split()[1] not in expected_responses:
                print "Unexpected MX record found!"
                print x.to_text().split()[1] + " OK!"


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

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

>>> ['', '']

Now you can compare against this list.

You mean:

x.to_text() in {'', ''}


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

txt = '20' # 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 = ['', '']
if False in [val in records for val in expected] or len(records) != len(expected):
    # Die.

What about?

import dns.resolver

expected_domains = set(['', ''])
domains = set(str( for mx in dns.resolver.query('', 'MX'))
if not domains.issuperset(expected_domains):
    print("Missing MX domains:", ", ".join(expected_domains - domains))

Need Your Help

Best way to share data among webservices

java spring cxf jax-ws

In my current scenario I have two webservices, hereby described: