Nov-28-2019, 08:31 AM
(Nov-28-2019, 07:23 AM)buran Wrote: well, that is error because it cannot resolve whatever remote host you pass as argument.
The problem is in the new line char at the end of each line when you read from file. I assume ABC is just placeholder and the file has real hostnames
import socket def get_host_name_ip(fname): with open(fname, "r") as f: f = ['google.com', 'google.com\n'] for remote_host in f: remote_host = remote_host.strip() # \n (new line) at the end of the line would cause error even when host exists print(remote_host) try: print(f"And IP address is {socket.gethostbyname(remote_host)}") except socket.gaierror as se: print(f"Not done: {se}") # this will catch error when socket.gethostbyname get_host_name_ip("computers.txt")
You are correct. Thank you so much, I appreciate that.