Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
socket loop problem
#1
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,
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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)
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(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,
Reply
#8
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")
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(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. Smile
Reply
#10
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Socket server problem H84Gabor 0 1,205 Jun-21-2022, 12:14 AM
Last Post: H84Gabor
  socket without blocking loop and automatically retrieve data from the port RubenP 3 3,487 Jun-21-2020, 10:59 PM
Last Post: Gribouillis
  problem in socket Mamad 2 2,320 Nov-10-2019, 11:44 AM
Last Post: j.crater
  socket problem technoplusnl 2 10,626 Dec-11-2016, 07:33 PM
Last Post: technoplusnl

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020