0
我有以下腳本需要適應從文件中讀取,具體而言,我需要拉動'域'和'預期響應'。目前他們是手動編寫在代碼中,因爲我沒有足夠的技巧來更有效地重構它。修改腳本以從文本文件中讀取
#!/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!"
我首先想到的是使用一個txt文件,但我無法弄清楚如何從我的Python知識同一個文件的域和expected_responses區分。我搜索了一下,並閱讀了關於import csv函數,這似乎是最好的選擇,但我沒有任何使用它的經驗。
任何人都可以爲我提供一個如何將import csv應用於我的代碼的例子嗎?
問候
克里斯
http://docs.python.org/library/csv.html –