python_hosts.hosts module¶
This module contains classes: HostsEntry: A representation of a hosts file entry, i.e. a line containing an IP address and name(s), a comment, or a blank line/line separator.
Hosts: A representation of a hosts file, e.g. /etc/hosts and c:\windows\system32\drivers\etc\hosts for a linux or MS windows based machine respectively. Each entry being represented as an instance of the HostsEntry class.
- class python_hosts.hosts.Hosts(path=None, entries=None)[source]¶
Bases:
objectA hosts file.
- add(entries=None, force=False, allow_address_duplication=False, allow_name_duplication=False, merge_names=False)[source]¶
Add instances of HostsEntry to the instance of Hosts. :param entries: A list of instances of HostsEntry :param force: Remove matching before adding :param allow_address_duplication: Allow using multiple entries
for same address
- Parameters:
allow_name_duplication – Allow using multiple entries for same name
merge_names – Merge names where address already exists
- Returns:
The counts of successes and failures
- static determine_hosts_path(platform=None)[source]¶
Return the hosts file path based on the supplied or detected platform. :param platform: a string used to identify the platform :return: detected filesystem path of the hosts file
- entries¶
- exists(address=None, names=None, comment=None)[source]¶
- Determine if the supplied address and/or names, or comment, exists in
a HostsEntry within Hosts
- Parameters:
address – An ipv4 or ipv6 address to search for
names – A list of names to search for
comment – A comment to search for
- Returns:
True if a supplied address, name, or comment is found. Otherwise, False.
- find_all_matching(address=None, name=None, comment=None)[source]¶
- Return all HostsEntry instances from the Hosts object
where the supplied ip address or name matches
- Parameters:
address – An ipv4 or ipv6 address
name – A host name
comment – A host inline comment
- Returns:
HostEntry instances
- static get_hosts_by_url(url=None)[source]¶
Request the content of a URL and return the response :param url: The URL of the hosts file to download :return: The content of the passed URL
- import_file(import_file_path=None)[source]¶
Read a list of host entries from a file, convert them into instances of HostsEntry and then append to the list of entries in Hosts :param import_file_path: The path to the file containing the host entries :return: Counts reflecting the attempted additions
- import_url(url=None, force=None)[source]¶
- Read a list of host entries from a URL, convert them into instances
of HostsEntry and then append to the list of entries in Hosts
- Parameters:
url – The URL of where to download a hosts file
- Returns:
Counts reflecting the attempted additions
- path¶
- populate_entries()[source]¶
- Called by the initialiser of Hosts. This reads the entries from the
local hosts file, converts them into instances of HostsEntry and adds them to the Hosts list of entries.
- Returns:
None
- class python_hosts.hosts.HostsEntry(entry_type=None, address=None, comment=None, names=None)[source]¶
Bases:
objectAn entry in a hosts file.
- address¶
- comment¶
- entry_type¶
- static get_entry_type(hosts_entry=None)[source]¶
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’
- names¶