Reputation: 904
I have the following script that I need to adapt to read from a file, specifically I need to pull the 'domain' and 'expected responses'. currently they are manually written in the code as I'm not skilled enough to refactor it more efficiently.
#!/usr/bin/python
import dns.resolver
from smtplib import SMTP
import datetime
debuglevel = 0
domain = 'exampledomain.co.uk'
expected_responses = ['example.co.uk.', 'example2.co.uk.']
for x in dns.resolver.query(domain, 'MX'):
if x.to_text().split()[1] not in expected_responses:
print "Unexpected MX record found!"
smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('localhost', 25)
from_addr = "MX ERROR <[email protected]>"
to_addr = "[email protected]"
subj = "MX ERROR"
date = datetime.datetime.now().strftime( "%d/%m/%Y %H:%M" )
message_text = "Dearest colleagues\nSomething appears to be wrong with the MX records for example.co.uk\n\nDON'T PANIC!\n"
msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, date, message_text )
smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()
else:
print x.to_text().split()[1] + " example.uk MX records OK!"
My first thought was to use a .txt file but I couldn't figure out how to differentiate between the domain and expected_responses from the same file with my python knowledge. I had a search about and read about the import csv function, which seems the best option but I don't have any experience using it.
Would anyone be able to provide me with an example of how I could apply import csv to my code?
Regards
Chris
Upvotes: 0
Views: 152
Reputation: 1656
http://docs.python.org/library/configparser.html
This may be more what you're looking for. Using an *.ini file seems to be a pretty standard practice for configuration files.
Upvotes: 1