Debian Codegraph @

examples/mxquery.py

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