socket loop problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: socket loop problem (/thread-22791.html) |
socket loop problem - monamour - Nov-27-2019 Dear All, I have tried to write a code which fetches the Host name and the IP Address for a user. The program is working nice as you can see below in the code, import socket def get_Host_name_IP(): remote_host =input("Please put the pc name: ") try: host_ip = socket.gethostbyname(remote_host) print ("The host name is: %s" %remote_host) print("IP address is %s : " %host_ip) except: print("Unable to get hostname or IP") get_Host_name_IP() However, when I try to use FOR loop to pass the parameter for more than one user through a text file which name is computer.txt is not showing as expected and drop the exception message instead Please see the code below, Suppose the computer.txt is containing two names which are ABC & XYZ import socket def get_Host_name_IP(): OsPcs = open("computers.txt", "r") Osdata = OsPcs.readlines() for i in Osdata: ##Here is printing the hostname without error print(i) try: ### the following line is not execute print("And IP address is %s : " %socket.gethostbyname(i)) except: print("Not done") get_Host_name_IP()The output is: ABC Not done XYZ Not done Process finished with exit code 0Any ideas? Many thanks, RE: socket loop problem - buran - Nov-27-2019 don't use general, all-catch ' except .this will prevent from showing what error is. You should be specific which error you expect and want to handle. At the moment you mask what the error is and make it impossible to resolve the problem. while developing remove the try except and see what error you get. If you need help in resolving that specific error - post full traceback in error tags. RE: socket loop problem - monamour - Nov-27-2019 I am really sorry this is the error as follows,
(Nov-27-2019, 09:46 AM)buran Wrote: don't use general, all-catch ' I am really sorry this is the error:
RE: socket loop problem - buran - Nov-27-2019 As the error message show, your code from first post is different from the actual code you run as you can see, you have second argument - 0 that cause an error. The error message is clear
RE: socket loop problem - monamour - Nov-27-2019 (Nov-27-2019, 10:59 AM)buran Wrote: As the error message show, your code from first post is different from the actual code you run Sorry dear because I am newbie in Python programming. The new error is showing below:
RE: socket loop problem - buran - Nov-27-2019 it's not new error. it's the same one. You have following line in the code that you run print("And IP address is %s : " %socket.gethostbyname(i,0))note that you pass 2 (two) arguments to socket.gethostbyname() - i and 0 . It expect just one - the remote host i . that is what the error message says Also note that 0 is not in the original code in post#1
RE: socket loop problem - monamour - Nov-28-2019 (Nov-27-2019, 11:40 AM)buran Wrote: it's not new error. it's the same one. You have following line in the code that you run Hi, the eror as following, Many thanks,
RE: socket loop problem - buran - Nov-28-2019 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: 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") RE: socket loop problem - monamour - Nov-28-2019 (Nov-28-2019, 07:23 AM)buran Wrote: well, that is error because it cannot resolve whatever remote host you pass as argument. You are correct. Thank you so much, I appreciate that. RE: socket loop problem - buran - Nov-28-2019 I am sure you understand that f = ['google.com', 'google.com\n'] should be removed :-) i.e. it's a left-over from me testing the script. I edited my original post
|