1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#!/usr/bin/python3
"""
A command-line application that uses Twisted to do an MX DNS query.
"""
from __future__ import print_function
from twisted.names.client import lookupMailExchange
from crochet import setup, wait_for
setup()
# Twisted code:
def _mx(domain):
"""
Return Deferred that fires with a list of (priority, MX domain) tuples for
a given domain.
"""
def got_records(result):
return sorted(
[(int(record.payload.preference), str(record.payload.name))
for record in result[0]])
d = lookupMailExchange(domain)
d.addCallback(got_records)
return d
# Blocking wrapper:
@wait_for(timeout=5)
def mx(domain):
"""
Return list of (priority, MX domain) tuples for a given domain.
"""
return _mx(domain)
# Application code:
def main(domain):
print("Mail servers for %s:" % (domain,))
for priority, mailserver in mx(domain):
print(priority, mailserver)
if __name__ == '__main__':
import sys
main(sys.argv[1])
|