summaryrefslogtreecommitdiff
path: root/project2/proj2_s4498062/dns_client.py
blob: ecc3f3de1e83d881e31ac900513ea396035ac308 (plain) (blame)
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
#!/usr/bin/env python2
""" Simple DNS client

A simple example of a client using the DNS resolver.
"""

import dns.resolver


def main():
    """Run the client"""
    # Parse arguments
    import argparse
    parser = argparse.ArgumentParser(description="DNS Client")
    parser.add_argument("hostname", help="hostname to resolve")
    parser.add_argument(
        "-c", "--caching", action="store_true",
        help="Enable caching")
    parser.add_argument(
        "-t", "--ttl", metavar="time", type=int, default=0,
        help="TTL value of cached entries")
    args = parser.parse_args()

    # Resolve hostname
    resolver = dns.resolver.Resolver(["localhost"], 15, args.caching, args.ttl)
    hostname, aliases, addresses = resolver.gethostbyname(args.hostname)

    # Print output
    print hostname
    print list(aliases)
    print list(addresses)

if __name__ == "__main__":
    main()