createResolver() 扭曲不工作

createResolver() in twisted not working

我有一个简单的代理脚本,我在其中使用 installResolver 函数来选择脚本读取其主机文件的位置:

from twisted.internet import reactor
from twisted.web import proxy, server
from twisted.python import log
from twisted.names import client

def createResolver(servers=None, resolvconf=None, hosts=None):
    if platform.getType() == 'posix':
        if resolvconf is None:
            resolvconf = b'/etc/resolv.conf'
        if hosts is None:
            hosts = b'/root/example'
        theResolver = Resolver(resolvconf, servers)
        hostResolver = hostsModule.Resolver(hosts)
    else:
        if hosts is None:
            hosts = r'c:\windows\hosts'
        from twisted.internet import reactor
        bootstrap = _ThreadedResolverImpl(reactor)
        hostResolver = hostsModule.Resolver(hosts)
        theResolver = root.bootstrap(bootstrap, resolverFactory=Resolver)

    L = [hostResolver, cache.CacheResolver(), theResolver]
    return resolve.ResolverChain(L)

site = server.Site(proxy.ReverseProxyResource('www.example.com', 80, ''.encode("utf-8")))
reactor.listenTCP(80, site)
reactor.run()

每当我 运行 这个脚本时,它就会超时并且不起作用。谁能告诉我我做错了什么?谢谢!!

createResolver() in twisted not working

你需要传入你想让它读取的主机文件,你需要确保它是可读的,即它在哪里 /root/example 意味着你需要 运行 作为 root 才能读取它。您不需要重写该方法。我明天安装试试。

创建一个文件/tmp/example,其中包含一个条目,即我使用

127.0.0.6 twistfoo.com

这是工作代码

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

def do_lookup(domain):
    hosts = "/tmp/example"
    resolver = client.createResolver(servers=None, resolvconf=None, hosts="/tmp/example")
    d = resolver.getHostByName(domain)
    d.addBoth(lookup_done)

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

domain = b'twistfoo.com'
reactor.callLater(0, do_lookup, domain)
reactor.run()

运行 在命令行上执行此操作,您将看到输出

127.0.0.6