简体   繁体   中英

socket.gethostbyaddr() returns error on some computers and not for others

I've looked for any other threads related to this topic, but after an extensive search i was not able to find an answer that relates to my question. Using Python, I'm trying to use socket.gethostbyaddr("ip here") to determine the hostname of an ip address in a local network:

import socket

def gethostname(ip):
    hostname = socket.gethostbyaddr(ip)

    return hostname

For some computers (such as the server) this returns the triplet of hostname, alias and other IP's, but for others it does not. Instead, i get the following error:

socket.herror: [Errno 4] No address associated with name

What exactly does this error imply? What could it be that causes it? Is there any service or instane that should be running on the target computer in order for this to work? The computers i'm trying to get the hostname of run Debian.

If this question has already been asked then i am sorry, but i could not find it.

If it has something to do with reverse dns lookups, how would i solve this?

It means exactly what it says, there is no address associated. Not all IP addresses have a reverse-lookup address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM