当前位置:  开发笔记 > 编程语言 > 正文

如何在Python中进行DNS查找,包括引用/ etc/hosts?

如何解决《如何在Python中进行DNS查找,包括引用/etc/hosts?》经验,为你挑选了2个好方法。

dnspython会非常好地进行我的DNS查找,但它完全忽略了内容/etc/hosts.

是否有一个python库调用,它会做正确的事情?即先检查etc/hosts,否则只回退到DNS查找?



1> Jochen Ritze..:

我真的不知道,如果你想要做的DNS查找自己或者如果你只是想要一台主机的IP地址.如果你想要后者,

import socket
print(socket.gethostbyname('localhost')) # result from hosts file
print(socket.gethostbyname('google.com')) # your os sends out a dns query


确实.Bortzmeyer的代码支持IPv4和IPv6.

2> bortzmeyer..:

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))]


很高兴添加转换步骤.`addrs = [str(i [4] [0])for for socket.getaddrinfo(name,80)]`给出了ips的列表.
推荐阅读
雯颜哥_135
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有