Reference:http://www.pythonforbeginners.com/python/dns-lookup-python
Looking at the reference above is much easier than reading the actual socket documentation. My purpose is to find a python way to resolve a group of hostnames and return them as ip addresses.
Here’s the same code.
import logging,socket logging.basicConfig(filename="dns.log",level=logging.INFO) hostnames = [] answers = [] hostnames = input("Enter hostnames with comma: ").split(",") for hostname in hostnames: try: answer = socket.gethostbyname(hostname) answers.append(answer) logging.info("Getting resolution answers from DNS for hostname {}".format(hostname)) except Exception as e: logging.error(e) exit(1) for i in answers: print(i) logging.info("Listing ip addresses...")
Here’s a test:
Enter hostnames with comma: www.google.com,www.hotmail.com,pool.ntp.org,cyruslab.net
74.125.24.105
204.79.197.212
139.99.107.37
192.0.78.24