This is a python library for managing a hosts file. It enables you to add and remove entries, or import them from a file or URL.
The docs are hosted on RTD (Read The Docs) here:
http://python-hosts.readthedocs.org/en/latest/index.html.
pip install python-hosts
Adding an entry to a hosts file
from python_hosts import Hosts, HostsEntry
hosts = Hosts(path='hosts_test')
new_entry = HostsEntry(entry_type='ipv4', address='1.2.3.4', names=['www.example.com', 'example'])
hosts.add([new_entry])
hosts.write()
Importing a list of host entries by URL
from python_hosts import Hosts, HostsEntry
hosts = Hosts(path='hosts_test')
hosts.import_url(url='https://dl.dropboxusercontent.com/u/167103/hosts')
hosts.write()
Tested on python 2.6, 2.7, 3.4, 3.5, pypy and pypy3
MIT