File manager - Edit - /home/newsbmcs.com/public_html/static/img/logo/hosts.py.tar
Back
usr/lib/python3/dist-packages/twisted/names/hosts.py 0000644 00000011307 15030164273 0016551 0 ustar 00 # -*- test-case-name: twisted.names.test.test_hosts -*- # Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ hosts(5) support. """ from twisted.internet import defer from twisted.internet.abstract import isIPAddress, isIPv6Address from twisted.names import common, dns from twisted.python import failure from twisted.python.compat import nativeString from twisted.python.filepath import FilePath def searchFileForAll(hostsFile, name): """ Search the given file, which is in hosts(5) standard format, for addresses associated with a given name. @param hostsFile: The name of the hosts(5)-format file to search. @type hostsFile: L{FilePath} @param name: The name to search for. @type name: C{bytes} @return: L{None} if the name is not found in the file, otherwise a C{str} giving the address in the file associated with the name. """ results = [] try: lines = hostsFile.getContent().splitlines() except BaseException: return results name = name.lower() for line in lines: idx = line.find(b"#") if idx != -1: line = line[:idx] if not line: continue parts = line.split() if name.lower() in [s.lower() for s in parts[1:]]: try: maybeIP = nativeString(parts[0]) except ValueError: # Not ASCII. continue if isIPAddress(maybeIP) or isIPv6Address(maybeIP): results.append(maybeIP) return results def searchFileFor(file, name): """ Grep given file, which is in hosts(5) standard format, for an address entry with a given name. @param file: The name of the hosts(5)-format file to search. @type file: C{str} or C{bytes} @param name: The name to search for. @type name: C{bytes} @return: L{None} if the name is not found in the file, otherwise a C{str} giving the first address in the file associated with the name. """ addresses = searchFileForAll(FilePath(file), name) if addresses: return addresses[0] return None class Resolver(common.ResolverBase): """ A resolver that services hosts(5) format files. """ def __init__(self, file=b"/etc/hosts", ttl=60 * 60): common.ResolverBase.__init__(self) self.file = file self.ttl = ttl def _aRecords(self, name): """ Return a tuple of L{dns.RRHeader} instances for all of the IPv4 addresses in the hosts file. """ return tuple( dns.RRHeader(name, dns.A, dns.IN, self.ttl, dns.Record_A(addr, self.ttl)) for addr in searchFileForAll(FilePath(self.file), name) if isIPAddress(addr) ) def _aaaaRecords(self, name): """ Return a tuple of L{dns.RRHeader} instances for all of the IPv6 addresses in the hosts file. """ return tuple( dns.RRHeader( name, dns.AAAA, dns.IN, self.ttl, dns.Record_AAAA(addr, self.ttl) ) for addr in searchFileForAll(FilePath(self.file), name) if isIPv6Address(addr) ) def _respond(self, name, records): """ Generate a response for the given name containing the given result records, or a failure if there are no result records. @param name: The DNS name the response is for. @type name: C{str} @param records: A tuple of L{dns.RRHeader} instances giving the results that will go into the response. @return: A L{Deferred} which will fire with a three-tuple of result records, authority records, and additional records, or which will fail with L{dns.DomainError} if there are no result records. """ if records: return defer.succeed((records, (), ())) return defer.fail(failure.Failure(dns.DomainError(name))) def lookupAddress(self, name, timeout=None): """ Read any IPv4 addresses from C{self.file} and return them as L{Record_A} instances. """ name = dns.domainString(name) return self._respond(name, self._aRecords(name)) def lookupIPV6Address(self, name, timeout=None): """ Read any IPv6 addresses from C{self.file} and return them as L{Record_AAAA} instances. """ name = dns.domainString(name) return self._respond(name, self._aaaaRecords(name)) # Someday this should include IPv6 addresses too, but that will cause # problems if users of the API (mainly via getHostByName) aren't updated to # know about IPv6 first. # FIXME - getHostByName knows about IPv6 now. lookupAllRecords = lookupAddress usr/lib/python3/dist-packages/cloudinit/distros/parsers/hosts.py 0000644 00000005301 15030205623 0021113 0 ustar 00 # Copyright (C) 2012 Yahoo! Inc. # # Author: Joshua Harlow <harlowja@yahoo-inc.com> # # This file is part of cloud-init. See LICENSE file for license information. from io import StringIO from cloudinit.distros.parsers import chop_comment # See: man hosts # or https://linux.die.net/man/5/hosts # or https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/configtuning-configfiles.html # noqa class HostsConf: def __init__(self, text): self._text = text self._contents = None def parse(self): if self._contents is None: self._contents = self._parse(self._text) def get_entry(self, ip): self.parse() options = [] for (line_type, components) in self._contents: if line_type == "option": (pieces, _tail) = components if len(pieces) and pieces[0] == ip: options.append(pieces[1:]) return options def del_entries(self, ip): self.parse() n_entries = [] for (line_type, components) in self._contents: if line_type != "option": n_entries.append((line_type, components)) continue else: (pieces, _tail) = components if len(pieces) and pieces[0] == ip: pass elif len(pieces): n_entries.append((line_type, list(components))) self._contents = n_entries def add_entry(self, ip, canonical_hostname, *aliases): self.parse() self._contents.append( ("option", ([ip, canonical_hostname] + list(aliases), "")) ) def _parse(self, contents): entries = [] for line in contents.splitlines(): if not len(line.strip()): entries.append(("blank", [line])) continue (head, tail) = chop_comment(line.strip(), "#") if not len(head): entries.append(("all_comment", [line])) continue entries.append(("option", [head.split(None), tail])) return entries def __str__(self): self.parse() contents = StringIO() for (line_type, components) in self._contents: if line_type == "blank": contents.write("%s\n" % (components[0])) elif line_type == "all_comment": contents.write("%s\n" % (components[0])) elif line_type == "option": (pieces, tail) = components pieces = [str(p) for p in pieces] pieces = "\t".join(pieces) contents.write("%s%s\n" % (pieces, tail)) return contents.getvalue() # vi: ts=4 expandtab
| ver. 1.4 |
Github
|
.
| PHP 8.2.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings