[Solved] Python Error: socket.gaierror : [errno 11004] getaddrinfo failed error

Because we are doing some web data set processing, we need to resolve the domain name and organize Ip, so we use
Socket.getaddrinfo.

First, read the domain name from the previously processed domain name dataset
Secondly, loop through them one by one
Do the exception handling

在这里插入图片描述

But the results are obviously not as good as they should be, but when you manually put a single domain name in and resolve it, there is no problem.

在这里插入图片描述

I think the problem is a newline at the end of server_name.

So I need to remove the ‘\n’
在这里插入图片描述

To explain, there are two ways to remove line breaks here.
Method 1: With .sprip(‘\n’)
Method 2: [:-1] (because the line break is always in the last character)
I hope this will help and solve the problem smoothly and instantly.

def get_dns():
    domains = DataDispose.get_domain_list()
    for domain in domains:
        try:
            myaddr = socket.getaddrinfo(domain[:-1], 'http')
            print(myaddr)
        except Exception as e:
            print(e)
            continue

 

Read More: