createResolver() in twisted not working

橙三吉。 提交于 2019-12-14 02:46:17

问题


I have a simple proxy script where I use installResolver function to choose where the script reads its hosts file:

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()

Whenever I run this script, It just times out and doesn't work. Could anybody tell me what it is I'm doing wrong? thanks!!


回答1:


createResolver() in twisted not working

You need to pass in the hosts file you want it to read and you need to make sure that it's readable ie where it is /root/example means you need to be running as root to read it. You don't need to override the method. I'll install it tomorrow and try it.

Create a file /tmp/example with an entry in it ie I used

127.0.0.6 twistfoo.com

This is working code

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()

Run this on command line and you will see the output

127.0.0.6


来源:https://stackoverflow.com/questions/36971934/createresolver-in-twisted-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!