我正在尝试在 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() 获取