0

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

它正在处理远程地址,例如 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'])

它还在回溯 127.0.0.1 上工作:

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

但它不在本地地址 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

在 Linux 上:

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

用 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

这不是一个正确的 PTR 条目,但应该由 gethostbyaddr() 获取

4

1 回答 1

0

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

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

感谢 mata 的帮助和 larsks 的 zeroconf

于 2015-06-09T11:16:21.203 回答