gpt4 book ai didi

python - 使用 Python 的 socket.gethostbyaddr() 有困难

转载 作者:太空狗 更新时间:2023-10-29 20:19:13 24 4
gpt4 key购买 nike

我正在尝试在 python 中使用 socket.gethostbyaddr() 反向 dns IP 列表,它为某些值返回“Unknown Host”,但使用 dig相同的 ip 返回主机名。此外,dig 似乎比使用 python 模块要快得多,这有什么具体原因吗?

import socket

# This returns 'Unknown Host'
name, alias, addresslist = socket.gethostbyaddr('114.143.51.197')

最佳答案

来自 the comments ...

whereas dig -x 114.143.51.197+short gives me the hostname.

对不起,你错了。 114.143.51.197 没有 PTR 记录...因此 socket.gethostbyaddr() 应该抛出错误...要正确处理此用例,请添加 try/except 捕获 socket.herror 的子句

>>> def dns_ptr_lookup(addr):
... try:
... return socket.gethostbyaddr(addr)
... except socket.herror:
... return None, None, None
...
>>> # At this time, 4.2.2.2 has a valid PTR
>>> name,alias,addresslist = dns_ptr_lookup('4.2.2.2')
>>> print(name)
vnsc-bak.sys.gtei.net
>>>
>>> # At this time, 114.143.51.197 does NOT have a valid PTR
>>> name,alias,addresslist = dns_ptr_lookup('114.143.51.197')
>>> print(name)
None
>>>

DNS 反向查找 114.143.51.197...注意它没有有效的 PTR 记录

[mpenning@Bucksnort ~]$ dig @8.8.8.8 -x 114.143.51.197

; <<>> DiG 9.6-ESV-R4 <<>> @8.8.8.8 -x 114.143.51.197
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 4735
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0

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

;; AUTHORITY SECTION:
114.in-addr.arpa. 1800 IN SOA ns1.apnic.net. read-txt-record-of-zone-first-dns-admin.apnic.net. 17812 7200 1800 604800 172800

;; Query time: 182 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Tue Nov 22 05:11:36 2011
;; MSG SIZE rcvd: 134

[mpenning@Bucksnort ~]$ python
Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostbyaddr('114.143.51.197')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
socket.herror: (1, 'Unknown host')
>>>

这是一个有效的 PTR 记录应该是这样的......

[mpenning@Bucksnort ~]$ dig -x 4.2.2.2

; <<>> DiG 9.6-ESV-R4 <<>> -x 4.2.2.2
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 61856
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 3, ADDITIONAL: 1

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

;; ANSWER SECTION:
2.2.2.4.in-addr.arpa. 86400 IN PTR vnsc-bak.sys.gtei.net.

;; AUTHORITY SECTION:
2.4.in-addr.arpa. 86400 IN NS dnsauth2.sys.gtei.net.
2.4.in-addr.arpa. 86400 IN NS dnsauth1.sys.gtei.net.
2.4.in-addr.arpa. 86400 IN NS dnsauth3.sys.gtei.net.

;; ADDITIONAL SECTION:
dnsauth1.sys.gtei.net. 1800 IN A 4.2.49.2

;; Query time: 308 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Tue Nov 22 05:10:16 2011
;; MSG SIZE rcvd: 158

[mpenning@Bucksnort ~]$ python
Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostbyaddr('4.2.2.2')
('vnsc-bak.sys.gtei.net', [], ['4.2.2.2'])
>>>

关于python - 使用 Python 的 socket.gethostbyaddr() 有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7832264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com