2010-05-10 62 views
72

dnspython会很好地完成我的DNS查找,但它完全忽略/etc/hosts的内容。如何在Python中进行DNS查找,包括引用/ etc/hosts?

是否有一个python库调用会做正确的事情?即首先检查etc/hosts,然后才回退到DNS查找。

+0

我为此创建了一个问题:https://github.com/rthalley/dnspython/issues/149 – 2016-03-31 15:07:43

+0

dnspython不会实现这个。对于简单的正向查找,使用建议的'socket.gethostbyname',对于更复杂的查询,请使用dnspython。 – sebix 2016-07-03 09:08:50

回答

78

我不确定你是否想要自己做DNS查找你自己或者你只是想要一个主机的IP。如果你想要后者,

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

确实。 Bortzmeyer的代码具有IPv4和IPv6的能力。 – fmark 2010-05-12 08:28:35

+0

有没有人知道在哪一级查找缓存?在Python中?还是OS?或DNS服务器? – 2011-09-18 09:36:11

+0

@Simon不是由Python缓存,也不是操作系统。它取决于涉及的任何DNS服务器,如果它缓存或不。 - 一般而言:DNS仅由应用程序本身缓存,或通过解析解析链中解析DNS服务器缓存。 – 2013-05-18 06:07:00

76

Python中的正常名称解析工作正常。为什么你需要DNSpython呢。只要使用socketgetaddrinfo跟随配置为您的操作系统的规则(Debian的,它遵循​​:

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

将很好添加转换步骤。 'addrs = [str(i [4] [0])for socket.getaddrinfo(name,80)]'给我一个ips列表。 – Alex 2015-11-26 15:43:15

-2

我发现这种方式来扩展DNS RR主机名扩展到IP地址的列表,进成员的主机名列表:

#!/usr/bin/python 

def expand_dnsname(dnsname): 
    from socket import getaddrinfo 
    from dns import reversename, resolver 
    namelist = [ ] 
    # expand hostname into dict of ip addresses 
    iplist = dict() 
    for answer in getaddrinfo(dnsname, 80): 
     ipa = str(answer[4][0]) 
     iplist[ipa] = 0 
    # run through the list of IP addresses to get hostnames 
    for ipaddr in sorted(iplist): 
     rev_name = reversename.from_address(ipaddr) 
     # run through all the hostnames returned, ignoring the dnsname 
     for answer in resolver.query(rev_name, "PTR"): 
      name = str(answer) 
      if name != dnsname: 
       # add it to the list of answers 
       namelist.append(name) 
       break 
    # if no other choice, return the dnsname 
    if len(namelist) == 0: 
     namelist.append(dnsname) 
    # return the sorted namelist 
    namelist = sorted(namelist) 
    return namelist 

namelist = expand_dnsname('google.com.') 
for name in namelist: 
    print name 

,当我运行它,列出了一些1e100.net主机名:

0
list(map(lambda x: x[4][0], socket.getaddrinfo(\ 
    'www.example.com.',22,type=socket.SOCK_STREAM))) 

为您提供www.example.com的地址列表。 (ipv4和ipv6)

相关问题