{"library":"python-hosts","title":"Python Hosts File Manager","description":"A Python library for managing hosts files, allowing you to add, remove, import, and query entries. Version 1.1.2 was released on June 24, 2025, indicating an active maintenance status. It maintains compatibility with Python 2.7 and Python 3.5+, including PyPy variants.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install python-hosts"],"cli":null},"imports":["from python_hosts import Hosts","from python_hosts import HostsEntry"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"from python_hosts import Hosts, HostsEntry\nimport os\n\n# Create a Hosts instance, using a test file to avoid modifying the system hosts file directly\nhosts_file_path = os.path.join(os.getcwd(), 'hosts_test')\nmy_hosts = Hosts(path=hosts_file_path)\n\n# Add a new entry\nnew_entry = HostsEntry(entry_type='ipv4', address='127.0.0.1', names=['localhost.dev', 'myapp.local'])\nmy_hosts.add([new_entry])\n\n# Add another entry, demonstrating merging names\nadditional_entry = HostsEntry(entry_type='ipv4', address='192.168.1.100', names=['backend.api'])\nmy_hosts.add([additional_entry])\n\n# Merge names to an existing entry\nmerge_entry = HostsEntry(entry_type='ipv4', address='127.0.0.1', names=['dev.server'])\nmy_hosts.add([merge_entry], merge_names=True)\n\n# Write changes back to the file\nmy_hosts.write()\n\nprint(f\"Hosts file updated at: {hosts_file_path}\")\n\n# Verify content (optional)\nwith open(hosts_file_path, 'r') as f:\n    print(\"\\n--- Current hosts_test content ---\")\n    print(f.read())\n\n# Clean up the test file\nos.remove(hosts_file_path)","lang":"python","description":"This quickstart demonstrates how to initialize a `Hosts` object, add new entries using `HostsEntry`, and persist changes to a specified hosts file. For safety, it uses a temporary test file instead of the system's default hosts file. It also shows how to merge additional hostnames to an existing IP address entry.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}