Skip to main content

hostsmgr (Hosts Manager)

https://img.shields.io/pypi/v/hostsmgr.svg https://img.shields.io/travis/starofrainnight/hostsmgr.svg https://ci.appveyor.com/api/projects/status/lx6dwcisa6bolsqw?svg=true Documentation Status Updates

API for manage hosts file

Features

Usage

from hostsmgr import HostsMgr
from hostsmgr.hostsmgr import guess_hosts_path
from hostsmgr.conditions import Any, All, IPAddress, Host, InlineComment

mgr = HostsMgr()

# Load system hosts file
mgr.load(guess_hosts_path())

# Save hosts to another place (Must open with text mode !)
mgr.save(open('/etc/hosts.old', 'w'))

# Save hosts to string with hosts file format
hosts_string = mgr.saves()

# Find all hosts entries that with 127.0.0.1 address
entries = mgr.find(IPAddress('127.0.0.1'))

# Find all entries that contained specific host
entries = mgr.find(Host('localhost'))

# Find all entries that contained specificed ip address and host both
entries = mgr.find(IPAddress('127.0.0.1') & Host('localhost'))

# Find all entries that contained either hosts
entries = mgr.find(Host('ip6-localhost') | Host('localhost'))

# Find all entries that contained either hosts, another method
entries = mgr.find(Any(Host('ip6-localhost'), Host('localhost')))

# Find all entries that contained both hosts
entries = mgr.find(Host('ip6-localhost') & Host('localhost'))

# Find all entries that contained both hosts, another method
entries = mgr.find(All(Host('ip6-localhost'), Host('localhost')))

# Find all entries that contained target inline comment
entries = mgr.find(InlineComment('THIS_IS_A_TAG'))

# Find only one entry that contained target inline comment
entries = mgr.find(InlineComment('THIS_IS_A_TAG'), at_most=1)

# Remove an entry that found by find()
mgr.remove(entry)

# Remove all hosts from hosts entries
mgr.remove_hosts(['localhost', 'ip6-localhost'])

# Remove all entries by inline comment exactly matched
mgr.remove_by_inline_comment(InlineComment('TAG_FOR_EXAMPLE'))

# Remove all entries by inline comment partial matched
mgr.remove_by_inline_comment(InlineComment('TAG_FOR_EXAMPLE', partial=True))

Credits

This package was created with Cookiecutter and the PyPackageTemplate project template.

History

0.0.1 (2018-04-15)

  • First release on PyPI.

Metadata

Release files for hostsmgr 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hostsmgr 0.2.5
File Size Uploaded
hostsmgr-0.2.5.tar.gz 20.4 kB Details

Release files / hostsmgr-0.2.5.tar.gz

Download URL hostsmgr-0.2.5.tar.gz
Size 20.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f9b16c194af91ba945d3c191907161d280cc58f8c0395d3cbeeeca9f4f7e061c
BLAKE2b-256 checksum
How to use checksums
7b466809ed7f7717a334eb9ccd07161cd90676ebb8f99c9afa48a8518dc94c49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.5 This release

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page