Ticket #5529: dns-resolver-windows-hosts.3.patch

File dns-resolver-windows-hosts.3.patch, 3.4 KB (added by Jared, 8 years ago)

Implements 1-3 of the request changes to the previous patch

  • twisted/names/client.py

     
    543543
    544544    @type hosts: C{str} or C{None}
    545545    @param hosts: If not C{None}, an alternate hosts file to use. If C{None}
    546     on posix systems, /etc/hosts will be used. On windows, C:\windows\hosts
    547     will be used.
     546    on posix systems, /etc/hosts will be used. On windows,
     547    %SYSTEMROOT%/system32/drivers/etc/hosts will be used.
    548548
    549549    @rtype: C{IResolver}
    550550    """
     
    552552    if platform.getType() == 'posix':
    553553        if resolvconf is None:
    554554            resolvconf = '/etc/resolv.conf'
    555         if hosts is None:
    556             hosts = '/etc/hosts'
    557555        theResolver = Resolver(resolvconf, servers)
    558556        hostResolver = hostsModule.Resolver(hosts)
    559     else:
    560         if hosts is None:
    561             hosts = r'c:\windows\hosts'
     557    elif platform.isWindows():
    562558        from twisted.internet import reactor
    563559        bootstrap = _ThreadedResolverImpl(reactor)
    564560        hostResolver = hostsModule.Resolver(hosts)
  • twisted/names/hosts.py

     
    55"""
    66hosts(5) support.
    77"""
     8import os
    89
    910from twisted.names import dns
    1011from twisted.persisted import styles
    1112from twisted.python import failure
    1213from twisted.python.filepath import FilePath
     14from twisted.python.runtime import platform
    1315from twisted.internet import defer
    1416from twisted.internet.abstract import isIPAddress
    1517
     
    7072
    7173
    7274
     75def _findHostsFile():
     76    """
     77    Returns the location of the system hosts file for this platform
     78    """
     79    if platform.isWindows():
     80        return os.environ['SYSTEMROOT'] + r'\system32\drivers\etc\hosts'
     81    else:
     82        return '/etc/hosts'
     83
     84
     85
    7386class Resolver(common.ResolverBase, styles.Versioned):
    7487    """
    7588    A resolver that services hosts(5) format files.
     
    8497            self.typeToMethod[k] = getattr(self, v)
    8598
    8699
    87     def __init__(self, file='/etc/hosts', ttl = 60 * 60):
     100    def __init__(self, file=None, ttl = 60 * 60):
    88101        common.ResolverBase.__init__(self)
     102        if file is None: file = _findHostsFile()
    89103        self.file = file
    90104        self.ttl = ttl
    91105
  • twisted/names/test/test_hosts.py

     
    1111
    1212from twisted.names.dns import (
    1313    A, AAAA, IN, DomainError, RRHeader, Query, Record_A, Record_AAAA)
    14 from twisted.names.hosts import Resolver, searchFileFor, searchFileForAll
     14from twisted.names.hosts import (
     15    Resolver, searchFileFor, searchFileForAll, _findHostsFile)
    1516
    1617
    1718class SearchHostsFileTests(TestCase):
     
    7475        self.assertEqual(searchFileFor(hosts.path, 'localhost'), '::1')
    7576
    7677
     78    def test_findsSystemHostsFile(self):
     79        """
     80        Ensures that the system hosts file is correctly identified by
     81        looking up the value for the 'localhost' entry
     82        """
     83        self.assertNotEqual(
     84            None, searchFileFor(_findHostsFile(), "localhost"))
    7785
     86
    7887class SearchHostsFileForAllTests(TestCase):
    7988    """
    8089    Tests for L{searchFileForAll}, a helper which finds all addresses for a