Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
socket loop problem
#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


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,284 Jun-21-2022, 12:14 AM
Last Post: H84Gabor
  socket without blocking loop and automatically retrieve data from the port RubenP 3 3,647 Jun-21-2020, 10:59 PM
Last Post: Gribouillis
  problem in socket Mamad 2 2,426 Nov-10-2019, 11:44 AM
Last Post: j.crater
  socket problem technoplusnl 2 12,955 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