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 0
Any ideas?
Many thanks,
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.
I am really sorry this is the error as follows,
Error:
Traceback (most recent call last):
File "C:/host-ip.py", line 20, in <module>
get_Host_name_IP()
File "C:/host-ip.py", line 16, in get_Host_name_IP
print("And IP address is %s : " %socket.gethostbyname(i,0))
TypeError: gethostbyname() takes exactly 1 argument (2 given)
Process finished with exit code 1
(Nov-27-2019, 09:46 AM)buran Wrote: [ -> ]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.
I am really sorry this is the error:
Error:
Traceback (most recent call last):
File "C:/host-ip.py", line 20, in <module>
get_Host_name_IP()
File "C:/host-ip.py", line 16, in get_Host_name_IP
print("And IP address is %s : " %socket.gethostbyname(i,0))
TypeError: gethostbyname() takes exactly 1 argument (2 given)
Process finished with exit code 1
As the error message show, your code from first post is different from the actual code you run
Error:
print("And IP address is %s : " %socket.gethostbyname(i,0))
as you can see, you have second argument -
0
that cause an error. The error message is clear
(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
Error:
print("And IP address is %s : " %socket.gethostbyname(i,0))
as you can see, you have second argument - 0
that cause an error. The error message is clear
Sorry dear because I am newbie in Python programming. The new error is showing below:
Error:
Traceback (most recent call last):
File "C:/host-ip.py", line 20, in <module>
get_Host_name_IP()
File "C:/host-ip.py", line 16, in get_Host_name_IP
print("And IP address is %s : " %socket.gethostbyname(i,0))
TypeError: gethostbyname() takes exactly 1 argument (2 given)
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
Error:
TypeError: gethostbyname() takes exactly 1 argument (2 given)
Also note that 0 is not in the original code in post#1
(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
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 Error:
TypeError: gethostbyname() takes exactly 1 argument (2 given)
Also note that 0 is not in the original code in post#1
Hi, the eror as following,
Error:
cannot resolve hostname: ABC
[Errno 11004] getaddrinfo failed
cannot resolve hostname: XYZ
[Errno 11004] getaddrinfo failed
cannot resolve hostname:
[Errno 11004] getaddrinfo failed
Process finished with exit code 0
Many thanks,
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")
(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.

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