Python和ADNS,陷入无限循环

2024-09-26 17:52:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一些查询adns的代码。这个代码的问题是它卡住了,怎么会?我来解释一下:

  • 假设我的DNA列表是[“8.8.4.4”,“8.8.8.8”,“208.67.220.220”,“208.67.222.222”,“192.168.50.1”]
  • 它将从列表中弹出一个dns并对其进行查询,现在这意味着dns将以相反的顺序进行查询
  • 不管我做什么,它都不会显示它首先获取的dns的结果(在我们的例子中是192.168.50.1)
  • 我不确定dns是否会这样回复
    • 首先,我更改了DNS列表,只包含最后一个DNS服务器,代码执行良好
    • 第二,我使用了5个DNS服务器的旧列表,只不过最后一个是由我管理的,这样我就可以跟踪代码是否查询它,令我惊讶的是,查询确实发生了。在
    • 所以进行查询,我们得到了结果,但是由于某种原因,这个结果从来没有被插入到解析的主机中,并且由于这个结果没有被插入,它的长度将保持小于dnslist的长度,从而导致一个无限循环。在

你认为是什么导致了这个问题,以及如何解决它?在

代码执行结果

Inside class's init'
Data
host www.yahoo.com
dnslist length 5
intensity 1
Inside resolve()
inside finished_resolving()
Resolved : 0/5
Inside 'while not finished_resolving'
Queue: 0/1
Launching Querying for www.yahoo.com/1 on 192.168.50.1
Queue: 1/1
Launching Querying for www.yahoo.com/1 on 208.67.222.222
inside collect_results()
inside finished_resolving()
Resolved : 0/5
Inside 'while not finished_resolving'
------------------------ CLIPPED ----------------
Inside 'while not finished_resolving'
inside collect_results()
Inside collect_results's for query in self.adns.completed()
DNS used was208.67.222.222
Answered : (0, 'any-fp.wa1.b.yahoo.com', 1286169807, ('69.147.125.65', '67.195.160.76'))
Resolved www.yahoo.com to 69.147.125.65 using 208.67.222.222
Resolved hosts count1
And they are: 
{'208.67.222.222': '69.147.125.65'}


inside finished_resolving()
Resolved : 1/5
Inside 'while not finished_resolving'
Queue: 1/1
Launching Querying for www.yahoo.com/1 on 208.67.220.220
inside collect_results()
inside finished_resolving()
Resolved : 1/5
Inside 'while not finished_resolving'
-------------------------- CLIPPED --------------------
inside collect_results()
Inside collect_results's for query in self.adns.completed()
DNS used was208.67.220.220
Answered : (0, 'any-fp.wa1.b.yahoo.com', 1286169790, ('67.195.160.76', '69.147.125.65'))
Resolved www.yahoo.com to 67.195.160.76 using 208.67.220.220
Resolved hosts count2
And they are: 
{'208.67.222.222': '69.147.125.65', '208.67.220.220': '67.195.160.76'}


inside finished_resolving()
Resolved : 2/5
Inside 'while not finished_resolving'
Queue: 1/1
Launching Querying for www.yahoo.com/1 on 8.8.8.8
inside collect_results()
inside finished_resolving()
Resolved : 2/5
Inside 'while not finished_resolving'
-------------------------- CLIPPED --------------------
inside collect_results()
Inside collect_results's for query in self.adns.completed()
DNS used was8.8.8.8
Answered : (0, 'eu-fp.wa1.b.yahoo.com', 1286169758, ('87.248.122.122',))
Resolved www.yahoo.com to 87.248.122.122 using 8.8.8.8
Resolved hosts count3
And they are: 
{'208.67.222.222': '69.147.125.65', '208.67.220.220': '67.195.160.76', '8.8.8.8': '87.248.122.122'}


inside finished_resolving()
Resolved : 3/5
Inside 'while not finished_resolving'
Queue: 1/1
Launching Querying for www.yahoo.com/1 on 8.8.4.4
inside collect_results()
inside finished_resolving()
Resolved : 3/5
Inside 'while not finished_resolving'
-------------------- CLIPPED -------------------------------------
inside collect_results()
Inside collect_results's for query in self.adns.completed()
DNS used was8.8.4.4
Answered : (0, 'eu-fp.wa1.b.yahoo.com', 1286169757, ('87.248.122.122',))
Resolved www.yahoo.com to 87.248.122.122 using 8.8.4.4
Resolved hosts count4
And they are: 
{'208.67.222.222': '69.147.125.65', '208.67.220.220': '67.195.160.76', '8.8.8.8': '87.248.122.122', '8.8.4.4': '87.248.122.122'}


inside finished_resolving()
Resolved : 4/5
Inside 'while not finished_resolving'
inside collect_results()
inside finished_resolving()
Resolved : 4/5
---------------- CLIPPED -------------------------------

(最后一个块一直重复,直到系统开始挂起,负载达到24)

代码

测试.py

^{pr2}$

异步_域名系统.py

#!/usr/bin/python
#

import sys
import adns
from time import time

class AsyncResolver(object):
    def __init__(self, dnslist, host, record, intensity=10):

        """
        dnslist: a list of dns used to resolve
        host : hostname to resolve
        record: recordtype to resolve
        intensity: how many hosts to resolve at once
        """
        print "Inside class's init'"
        self.dnslist = dnslist
        self.host = host
        self.record = record


        if intensity >= len(dnslist) :
            self.intensity = len(dnslist)/5+1
        else:
            self.intensity = intensity

        print "Data"
        print "host " + host
        print "dnslist length " + str(len(dnslist))
        print "intensity " + str(intensity)




    def resolve(self):
        """ Resolves hosts and returns a dictionary of { 'dns': 'ip' }. """
        print "Inside resolve()"

        host = self.host
        record = self.record
        resolved_hosts = {}
        active_queries = {}
        dns_queue = self.dnslist[:]

        def collect_results():
            print "inside collect_results()"
            for query in self.adns.completed():
                print "Inside collect_results's for query in self.adns.completed()"
                answer = query.check()
                dns = active_queries[query]
                print "DNS used was" + dns
                print "Answered : " + str(answer)
                del active_queries[query]
                if answer[0] == 0:
                    #print "Inside answer[0] == 0 , ip:" + answer[3][0]
                    ip = answer[3][0]
                    resolved_hosts[dns] = ip
                    print "Resolved %s to %s using %s" % (host, ip, dns)
                    print "Resolved hosts count" + str(len(resolved_hosts))
                    print "And they are: "
                    print str(resolved_hosts)
                    print "\n"

                elif answer[0] == 101 and not record == adns.rr.CNAME: # CNAME if CNAME wasn't required'
                    print "ooopppps, i got a CNAME, gotta find its A"
                    print "\n"
                    query = self.adns.submit(answer[1], adns.rr.A)
                    active_queries[query] = dns
                else:
                    resolved_hosts[dns] = None
                    print "THIS COULD NOT BE RESOLVED"

        def finished_resolving():
            print "inside finished_resolving()"
            print "Resolved : " + str(len(resolved_hosts)) + "/" + str(len(self.dnslist))
            return len(resolved_hosts) == len(self.dnslist)

        while not finished_resolving():
            print "Inside 'while not finished_resolving'"
            while dns_queue and len(active_queries) <= self.intensity:
                print "Queue: " + str(len(active_queries)) + "/" + str(self.intensity)
                dns = dns_queue.pop()
                self.adns = adns.init(adns.iflags.noautosys,sys.stderr,"nameserver "+dns)
                query = self.adns.submit(host, record)
                print "Launching Querying for " + host + "/" + str(record) + " on " + dns
                active_queries[query] = dns
            collect_results()
        return resolved_hosts

Tags: selfcomdnsresultsyahoocollectprintfinished
2条回答

只要while not finished_resolving()返回True,您的while not finished_resolving()循环显然将继续。该函数只是比较len(resolved_hosts)和{}。但是,据我所见,你永远不会改变循环中这些变量的长度,所以它将永远持续下去。在

在注释后编辑这两个块中的任何一个都不会影响这两个变量的长度。请解释一下你认为它们是如何改变的。在

我刚刚尝试使用adns python绑定,并且遇到了类似的问题——completed querys数组永远不会被完全填满,导致check循环永远运行。在我的例子中,似乎没有将无法解析的域(由于NXDOMAIN等)添加到已完成的查询数组中(值为空/Null)

我查看了C绑定,但看不到用于保存失败结果的单独数据结构,因此似乎应该以某种方式将这些结果添加到“已完成”查询中。我怀疑绑定或adns库中有一个bug阻止了这种情况的发生。在

相关问题 更多 >

    热门问题