扭曲的dns不起作用

jac*_*ack 4 python twisted

我想知道为什么以下不起作用.

from twisted internet import defer, reactor
from twisted.python.failure import Failure
import twisted.names.client

def do_lookup(do_lookup):
    d = twisted.names.client.getHostByName(domain)
    d.addBoth(lookup_done)

def lookup_done(result):
    print 'result:', result
    reactor.stop()

domain = 'twistedmatrix.com'    
reactor.callLater(0, do_lookup, domain) 
reactor.run()
Run Code Online (Sandbox Code Playgroud)

结果是:

result: [Failure instance: Traceback
(failure with no frames): <class
'twisted.names.error.ResolverError'>:
Stuck at response without answers or
delegation ]
Run Code Online (Sandbox Code Playgroud)

Ger*_*rat 5

在撰写本文时,这在Windows上失败,因为它对windows主机文件使用了无效路径(在twisted.names.client.createResolver中.它使用'c:\ windows\hosts'.这对于Windows 98和我(在这里参考),但会失败,像XP一样"现代".

今天,应该使用类似的东西:

hosts = os.path.join(
                     os.environ.get('systemroot','C:\\Windows'),
                     r'system32\drivers\etc\hosts'
                    )
Run Code Online (Sandbox Code Playgroud)

认为这只能部分地解决问题(或者这可能是一个红鲱鱼).

这仅适用于此hosts文件中实际指定的名称.它可能需要做的是DNS服务器的某种注册表查询,然后查询实际的DNS名称查找.

这个配方看起来很有希望获得实际的DNS服务器.


Gly*_*yph 3

将您的示例更正为以下内容,使其在语法上有效:

from twisted.internet import reactor
import twisted.names.client

def do_lookup(domain):
    d = twisted.names.client.getHostByName(domain)
    d.addBoth(lookup_done)

def lookup_done(result):
    print 'result:', result
    reactor.stop()

domain = 'twistedmatrix.com'
reactor.callLater(0, do_lookup, domain)
reactor.run()
Run Code Online (Sandbox Code Playgroud)

我得到:

$ python so-example.py 
result: 66.35.39.65
Run Code Online (Sandbox Code Playgroud)

所以,回答你的问题:你本地的DNS环境被破坏了,而不是twisted.names。或者也许有一个错误。您需要进一步追踪它。

  • 它可以在Linux下运行。但在 Windows 中不起作用。我收到上述错误。它在Windows中寻找resolv.conf吗? (2认同)