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


Messages In This Thread
socket loop problem - by monamour - Nov-27-2019, 09:16 AM
RE: socket loop problem - by buran - Nov-27-2019, 09:46 AM
RE: socket loop problem - by monamour - Nov-27-2019, 10:12 AM
RE: socket loop problem - by buran - Nov-27-2019, 10:59 AM
RE: socket loop problem - by monamour - Nov-27-2019, 11:27 AM
RE: socket loop problem - by buran - Nov-27-2019, 11:40 AM
RE: socket loop problem - by monamour - Nov-28-2019, 04:49 AM
RE: socket loop problem - by buran - Nov-28-2019, 07:23 AM
RE: socket loop problem - by monamour - Nov-28-2019, 08:31 AM
RE: socket loop problem - by buran - Nov-28-2019, 12:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Socket server problem H84Gabor 0 1,261 Jun-21-2022, 12:14 AM
Last Post: H84Gabor
  socket without blocking loop and automatically retrieve data from the port RubenP 3 3,604 Jun-21-2020, 10:59 PM
Last Post: Gribouillis
  problem in socket Mamad 2 2,399 Nov-10-2019, 11:44 AM
Last Post: j.crater
  socket problem technoplusnl 2 12,240 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