简体   繁体   中英

python gethostbyaddr unknown host mac

I'm trying to use the socket function gethostbyaddr in python. It's working perfectly on Linux but not on OSX

It's working on distant address like 8.8.8.8

>>> socket.gethostbyaddr('8.8.8.8')
('google-public-dns-a.google.com', ['8.8.8.8.in-addr.arpa'], ['8.8.8.8'])

It's also working on lookback 127.0.0.1 :

>>> socket.gethostbyaddr('127.0.0.1')
('localhost', ['1.0.0.127.in-addr.arpa'], ['127.0.0.1'])

But it isn't on a local address 10.102.188.21 :

>>> socket.gethostbyaddr('10.102.188.21')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.herror: [Errno 1] Unknown host

On a Linux :

>>> print(socket.gethostbyaddr('10.102.188.21'))
('Pierre.local', [], ['10.102.188.21'])

Query with dig :

dig -x 10.102.188.21

; <<>> DiG 9.8.3-P1 <<>> -x 10.102.188.21
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 21064
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;21.188.102.10.in-addr.arpa.    IN  PTR

It isn't a proper PTR entry but it should be gotten by the gethostbyaddr()

On linux in the /etc/nsswitch.conf there is a mdns option that resolve the hostname. That's why Linux can resolve the hostname on local network

OS X is supposed to resolve it with mDNS also, as it's part of zeroconf

Thanks mata for the help and larsks for zeroconf

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