dnspython会非常好地进行我的DNS查找,但它完全忽略了内容/etc/hosts
.
是否有一个python库调用,它会做正确的事情?即先检查etc/hosts
,否则只回退到DNS查找?
我真的不知道,如果你想要做的DNS查找自己或者如果你只是想要一台主机的IP地址.如果你想要后者,
import socket print(socket.gethostbyname('localhost')) # result from hosts file print(socket.gethostbyname('google.com')) # your os sends out a dns query
Python中的普通名称解析工作正常.为什么你需要DNSpython.只要使用插座的getaddrinfo
下面配置为您的操作系统的规则(Debian的,它遵循/etc/nsswitch.conf
:
>>> print socket.getaddrinfo('google.com', 80) [(10, 1, 6, '', ('2a00:1450:8006::63', 80, 0, 0)), (10, 2, 17, '', ('2a00:1450:8006::63', 80, 0, 0)), (10, 3, 0, '', ('2a00:1450:8006::63', 80, 0, 0)), (10, 1, 6, '', ('2a00:1450:8006::68', 80, 0, 0)), (10, 2, 17, '', ('2a00:1450:8006::68', 80, 0, 0)), (10, 3, 0, '', ('2a00:1450:8006::68', 80, 0, 0)), (10, 1, 6, '', ('2a00:1450:8006::93', 80, 0, 0)), (10, 2, 17, '', ('2a00:1450:8006::93', 80, 0, 0)), (10, 3, 0, '', ('2a00:1450:8006::93', 80, 0, 0)), (2, 1, 6, '', ('209.85.229.104', 80)), (2, 2, 17, '', ('209.85.229.104', 80)), (2, 3, 0, '', ('209.85.229.104', 80)), (2, 1, 6, '', ('209.85.229.99', 80)), (2, 2, 17, '', ('209.85.229.99', 80)), (2, 3, 0, '', ('209.85.229.99', 80)), (2, 1, 6, '', ('209.85.229.147', 80)), (2, 2, 17, '', ('209.85.229.147', 80)), (2, 3, 0, '', ('209.85.229.147', 80))]