python gethostbyaddr未知主机mac

时间:2015-06-08 17:10:50

标签: python macos gethostbyaddr

我正在尝试在python中使用套接字函数gethostbyaddr。 它在Linux上完美运行,但在OSX上运行

它正在开发像8.8.8.8这样的远程地址

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

它还在回顾127.0.0.1:

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

但它不在本地地址10.102.188.21:

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

在Linux上:

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

使用dig查询:

{{1}}

这不是一个合适的PTR条目,但它应该由gethostbyaddr()获取

1 个答案:

答案 0 :(得分:0)

在/etc/nsswitch.conf中的linux上有一个mdns选项可以解析主机名。 这就是Linux可以解析本地网络上的主机名的原因

OS X应该用mDNS来解决它,因为它是zeroconf的一部分

感谢mata为zeroconf的帮助和larsks