E
E
Evreke2016-03-31 16:03:59
Python
Evreke, 2016-03-31 16:03:59

What is the correct way to work with socket.getbyhostname in a loop?

I have a text file which contains hostnames:
www.google.com
www.yandex.com
etc.
There is such a code

import socket

pathToAddressList = input('Type in path to .txt file with listed hosts')
addressList = open(pathToAddressList, 'r+')

for address in addressList:
    print(socket.gethostbyname(address))

If a single line is written in a text file, then the code works and displays the ip of the host. But if you add an additional hostname to the text file, then the code falls out with an error
Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/untitled/deleting directory/script.py", line 10, in <module>
    print(socket.gethostbyname(address))
socket.gaierror: [Errno 11004] getaddrinfo failed

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-04-01
@Evreke

socket.gethostbyname(address.strip())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question