DNSPython 异步解析超时,但同步解析不会超时

2024-02-17

我在 Python 3.9.7 x64、dnspython 2.1.0 和 Windows 10 21H1 上执行了测试。

问题很简单:

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

我可以向您保证这个问题不是由我的物理宽带连接引起的:

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

如何解决这个问题?


It 好像 https://github.com/rthalley/dnspython/issues/637dnspython 有一个错误,但已修复似乎没有 https://github.com/rthalley/dnspython/issues/637#issuecomment-957004804尚未在 PyPi 上发布。

我能建议的最好的办法是从 Github 安装版本:

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

有了它你的代码就可以工作了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DNSPython 异步解析超时,但同步解析不会超时 的相关文章

随机推荐