简体   繁体   English

DNSPython 异步解析超时,但同步解析没有

[英]DNSPython asynchronous resolve times out but synchronous resolve doesn't

I performed the tests on Python 3.9.7 x64, dnspython 2.1.0 and Windows 10 21H1.我在 Python 3.9.7 x64、dnspython 2.1.0 和 Windows 10 21H1 上进行了测试。

The problem is very simple:问题很简单:

dns.resolver.Resolver() works normally but not dns.asyncresolver.Resolver() , they are supposed to have the same API, however while the former does indeed return results, the latter always times out. dns.resolver.Resolver()工作正常,但dns.asyncresolver.Resolver() ,它们应该具有相同的 API 时间,但后者确实会超时。

Example:例子:

In [55]: import dns
    ...: sequential_resolver = dns.resolver.Resolver()
    ...: sequential_resolver.nameservers = ['8.8.8.8']

In [56]: ans = sequential_resolver.resolve('www.google.com')

In [57]: ans[0].address
Out[57]: '162.125.18.129'

In [58]: from dns.asyncresolver import Resolver

In [59]: resolver = Resolver()

In [60]: resolver.nameservers = ['8.8.8.8']

In [61]: asyncio.run(resolver.resolve('www.google.com'))
---------------------------------------------------------------------------
Timeout                                   Traceback (most recent call last)
<ipython-input-61-4971055f7c5e> in <module>
----> 1 asyncio.run(resolver.resolve('www.google.com'))

C:\Program Files\Python39\lib\asyncio\runners.py in run(main, debug)
     42         if debug is not None:
     43             loop.set_debug(debug)
---> 44         return loop.run_until_complete(main)
     45     finally:
     46         try:

C:\Program Files\Python39\lib\asyncio\base_events.py in run_until_complete(self, future)
    640             raise RuntimeError('Event loop stopped before Future completed.')
    641
--> 642         return future.result()
    643
    644     def stop(self):

C:\Program Files\Python39\lib\site-packages\dns\asyncresolver.py in resolve(self, qname, rdtype, rdclass, tcp, source, raise_on_no_answer, source_port, lifetime, search, backend)
     72                 if backoff:
     73                     await backend.sleep(backoff)
---> 74                 timeout = self._compute_timeout(start, lifetime)
     75                 try:
     76                     if dns.inet.is_address(nameserver):

C:\Program Files\Python39\lib\site-packages\dns\resolver.py in _compute_timeout(self, start, lifetime)
    995                 now = start
    996         if duration >= lifetime:
--> 997             raise Timeout(timeout=duration)
    998         return min(lifetime - duration, self.timeout)
    999

Timeout: The DNS operation timed out after 5.407369613647461 seconds

I can assure you this issue is not caused by my physical broadband connection:我可以向您保证,此问题不是由我的物理宽带连接引起的:

PS C:\Windows\System32> ping 8.8.8.8

Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=66ms TTL=48
Reply from 8.8.8.8: bytes=32 time=66ms TTL=48
Reply from 8.8.8.8: bytes=32 time=66ms TTL=48
Reply from 8.8.8.8: bytes=32 time=67ms TTL=48

Ping statistics for 8.8.8.8:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 66ms, Maximum = 67ms, Average = 66ms

How can this be solved?如何解决?

It looks like there's a bug in dnspython, but the fix doesn't seem to be released on PyPi yet.看起来dnspython 中有一个错误,但修复似乎还没有在 PyPi 上发布。

The best thing I can advise is to install the version from Github:我能建议的最好的事情是从 Github 安装版本:

pip install -U https://github.com/rthalley/dnspython/archive/master.zip

With it your code will work.有了它,你的代码就可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM