Пример #1
0
 def str_to_hostentry(entry, sanitize=False, single_format=False):
     """
     Transform a line from a hosts file into an instance of HostsEntry
     :param entry: A line from the hosts file
     :return: An instance of HostsEntry
     """
     line_parts = entry.strip().split()
     address_part = line_parts[0]
     if is_ipv4(address_part) and valid_hostnames(line_parts[1:]):
         if sanitize:
             address_part = '0.0.0.0'
         return HostsEntry(entry_type='ipv4',
                           address=address_part,
                           names=line_parts[1:])
     elif is_ipv6(address_part) and valid_hostnames(line_parts[1:]):
         if sanitize:
             address_part = '::'
         return HostsEntry(entry_type='ipv6',
                           address=address_part,
                           names=line_parts[1:])
     elif single_format and len(line_parts) == 1 and valid_hostnames(
             line_parts):
         address_part = '0.0.0.0'
         return HostsEntry(entry_type='ipv4',
                           address=address_part,
                           names=line_parts)
     else:
         return False
Пример #2
0
 def get_entry_type(hosts_entry=None):
     """
     Return the type of entry for the line of hosts file passed
     :param hosts_entry: A line from the hosts file
     :return: 'comment' | 'blank' | 'ipv4' | 'ipv6'
     """
     if hosts_entry and isinstance(hosts_entry, str):
         entry = hosts_entry.strip()
         if not entry or not entry[0] or entry[0] == "\n":
             return 'blank'
         if entry[0] == "#":
             return 'comment'
         entry_chunks = entry.split()
         if is_ipv6(entry_chunks[0]):
             return 'ipv6'
         if is_ipv4(entry_chunks[0]):
             return 'ipv4'
Пример #3
0
 def str_to_hostentry(entry):
     """
     Transform a line from a hosts file into an instance of HostsEntry
     :param entry: A line from the hosts file
     :return: An instance of HostsEntry
     """
     line_parts = entry.strip().split()
     if is_ipv4(line_parts[0]) and valid_hostnames(line_parts[1:]):
         return HostsEntry(entry_type='ipv4',
                           address=line_parts[0],
                           names=line_parts[1:])
     elif is_ipv6(line_parts[0]) and valid_hostnames(line_parts[1:]):
         return HostsEntry(entry_type='ipv6',
                           address=line_parts[0],
                           names=line_parts[1:])
     else:
         return False
Пример #4
0
 def get_entry_type(hosts_entry=None):
     """
     Return the type of entry for the line of hosts file passed
     :param hosts_entry: A line from the hosts file
     :return: 'comment' | 'blank' | 'ipv4' | 'ipv6'
     """
     if hosts_entry and isinstance(hosts_entry, str):
         entry = hosts_entry.strip()
         if not entry or not entry[0] or entry[0] == "\n":
             return 'blank'
         if entry[0] == "#":
             return 'comment'
         entry_chunks = entry.split()
         if is_ipv6(entry_chunks[0]):
             return 'ipv6'
         if is_ipv4(entry_chunks[0]):
             return 'ipv4'
Пример #5
0
 def str_to_hostentry(entry):
     """
     Transform a line from a hosts file into an instance of HostsEntry
     :param entry: A line from the hosts file
     :return: An instance of HostsEntry
     """
     line_parts = entry.strip().split()
     if is_ipv4(line_parts[0]) and valid_hostnames(line_parts[1:]):
         return HostsEntry(entry_type='ipv4',
                           address=line_parts[0],
                           names=line_parts[1:])
     elif is_ipv6(line_parts[0]) and valid_hostnames(line_parts[1:]):
         return HostsEntry(entry_type='ipv6',
                           address=line_parts[0],
                           names=line_parts[1:])
     else:
         return False
Пример #6
0
    def __init__(self,
                 entry_type=None,
                 address=None,
                 comment=None,
                 names=None):
        """
        Initialise an instance of a Hosts file entry
        :param entry_type: ipv4 | ipv6 | comment | blank
        :param address: The ipv4 or ipv6 address belonging to the instance
        :param comment: The comment belonging to the instance
        :param names: The names that resolve to the specified address
        :return: None
        """
        if not entry_type or entry_type not in ('ipv4',
                                                'ipv6',
                                                'comment',
                                                'blank'):
            raise Exception('entry_type invalid or not specified')

        if entry_type == 'comment' and not comment:
            raise Exception('entry_type comment supplied without value.')

        if entry_type == 'ipv4':
            if not all((address, names)):
                raise Exception('Address and Name(s) must be specified.')
            if not is_ipv4(address):
                raise InvalidIPv4Address()

        if entry_type == 'ipv6':
            if not all((address, names)):
                raise Exception('Address and Name(s) must be specified.')
            if not is_ipv6(address):
                raise InvalidIPv6Address()

        self.entry_type = entry_type
        self.address = address
        self.comment = comment
        self.names = names
Пример #7
0
    def __init__(self,
                 entry_type=None,
                 address=None,
                 comment=None,
                 names=None):
        """
        Initialise an instance of a Hosts file entry
        :param entry_type: ipv4 | ipv6 | comment | blank
        :param address: The ipv4 or ipv6 address belonging to the instance
        :param comment: The comment belonging to the instance
        :param names: The names that resolve to the specified address
        :return: None
        """
        if not entry_type or entry_type not in ('ipv4',
                                                'ipv6',
                                                'comment',
                                                'blank'):
            raise Exception('entry_type invalid or not specified')

        if entry_type == 'comment' and not comment:
            raise Exception('entry_type comment supplied without value.')

        if entry_type == 'ipv4':
            if not all((address, names)):
                raise Exception('Address and Name(s) must be specified.')
            if not is_ipv4(address):
                raise InvalidIPv4Address()

        if entry_type == 'ipv6':
            if not all((address, names)):
                raise Exception('Address and Name(s) must be specified.')
            if not is_ipv6(address):
                raise InvalidIPv6Address()

        self.entry_type = entry_type
        self.address = address
        self.comment = comment
        self.names = names
Пример #8
0
def test_ipv6_validation_failure():
    """
    Test function returns False if invalid IPv4 address is specified
    """
    assert not is_ipv6('2001::0234:C1ab::A0:aabc:003F')
Пример #9
0
def test_ipv6_validation_success():
    """
    Test function returns True if valid IPv4 address is specified
    """
    assert is_ipv6('2001:db8::ff00:42:8329')