使用adns库解析域名

1. adns、adns-python库简介html

adns库是一个可进行异步非阻塞解析域名的库,主要使用C语言编写,在linux平台下运行。使用adns库进行域名解析效率很是,著名的开源网络爬虫larbin就使用adns库进行域名解析。惋惜的是,adns库没有说明文档。做者的意思是,adns.h这个文件便可说明这个库的使用方法。很是遗憾,我不太懂dns解析过程当中所涉及到的各类细节知识,对C语言的掌握程度也没能达到出神入画的境界,因此,我不得不认可,光凭这个adns.h,我没法知道应该如何使用adns库这一强大的工具。python

adns-python库是adns库的python扩展。在python程序中,可调用其余语言写好的函数。因为adns比较优秀,因此有人为它开发了一个python扩展程序。查了下Python扩展程序的相关内容,了解到写这种扩展程序是有模板的。首先,应该在file1.c文件中把核心函数写好。而后,再开发一个为这些核心文件开发python扩展模块文件(也为一个.c文件)。一般,扩展模块至少应该包含三个部分:导出函数、方法列表和初始化函数。python扩展模块保存在file2.c文件中。最后,经过编译,将这些核心函数编译成一个python程序可调用的函数。这么提及来比较抽象,能够参考这篇文章(http://www.ibm.com/developerworks/cn/linux/l-pythc/)或者《python核心编程(第二版)》中的第22章“扩展python”来清楚地了解Python是如何扩展C程序的。linux

目前,IPv4协议是互联网上的主流IP层的协议。但因为IPv4地址即将耗尽,各方都在积极推动IPv6协议。adns/adns-python协议也分为IPv4版和IPv6版。下载地址以下git

adns IPv4版    adns IPv6版(1.4版可支持IPv6)    adns-python IPv4版    adns-python IPv6版github

2.python-adns库的安装编程

由于我使用python进行开发,因此使用adns-python做为个人库。因为dns解析的核心函数都在adns中,因此必须首先安装adns库后,adns-python才能够安装成功。若是想进行IPv6的地址解析,必须安装支持IPv6地址解析的adns库和adns-python库。支持IPv4的库不可解析IPv6的域名地址,但支持IPv6的库可同时支持解析IPv4和IPv6的域名地址。adns库的源代码中的INSTALL文件有说明如何安装adns库,adns-python中的README文件中也有说明如何安装adns-python库。支持IPv4和支持IPv6的库安装步骤相似。vim

安装adns库:
$ ./configure [--disable-dynamic] [--prefix=...]
$ make
# make install
注意,make install这一命令须要有root权限

安装adns-python库:
$ python setup.py build (若是提示没有python.h文件,请安装python-devel。 命令为: yum install python-devel)
# python setup.py install
注意,python setup.py install这一命令须要有root权限

 3. python-adns库的使用网络

这里有一个利用adns-python库写的dns解析的代码。起先我不太明白为何做者就能知道应该这样使用这些库中的函数,由于adns-python库与adns库同样,没有说明文档。后来,经高人指点,发现adns-python库中有一个ADNS.py文件,这个文件中QueryEngine类,这个类中定义的函数即给咱们示范了库中提供的经常使用函数的使用方法。至于这些经常使用函数具备什么样的功能,应该如何使用等问题,只能有去源代码中找寻答案啦~~异步

若是代码报错,提示:libadns.so.1: cannot open shared object file: No such file or directory. 能够这样解决:async

1. vim /etc/ld.so.conf
2. 添加该lib所在的路径 (libadns.so.1的路径一般中/usr/local/lib )
3. ldconfig

这是由于libadns.so.1不在默认共享库路径下。具体可参看:http://www.cnblogs.com/xuxm2007/archive/2010/08/10/1796254.html

把人家的代码贴在这里好啦:

#!/usr/bin/python
#

import adns
from time import time

class AsyncResolver(object):
    def __init__(self, hosts, intensity=100):
        """
        hosts: a list of hosts to resolve
        intensity: how many hosts to resolve at once
        """
        self.hosts = hosts
        self.intensity = intensity
        self.adns = adns.init()

    def resolve(self):
        """ Resolves hosts and returns a dictionary of { 'host': 'ip' }. """
        resolved_hosts = {}
        active_queries = {}
        host_queue = self.hosts[:]

        def collect_results():
            for query in self.adns.completed():
                answer = query.check()
                host = active_queries[query]
                del active_queries[query]
                if answer[0] == 0:
                    ip = answer[3][0]
                    resolved_hosts[host] = ip
                elif answer[0] == 101: # CNAME
                    query = self.adns.submit(answer[1], adns.rr.A)
                    active_queries[query] = host
                else:
                    resolved_hosts[host] = None

        def finished_resolving():
            return len(resolved_hosts) == len(self.hosts)

        while not finished_resolving():
            while host_queue and len(active_queries) < self.intensity:
                host = host_queue.pop()
                query = self.adns.submit(host, adns.rr.A)
                active_queries[query] = host
            collect_results()

        return resolved_hosts

if __name__ == "__main__":
    host_format = "www.host%d.com"
    number_of_hosts = 20000

    hosts = [host_format % i for i in range(number_of_hosts)]

    ar = AsyncResolver(hosts, intensity=500)
    start = time()
    resolved_hosts = ar.resolve()
    end = time()

    print "It took %.2f seconds to resolve %d hosts." % (end-start, number_of_hosts)
相关文章
相关标签/搜索