can't add ipv6 entry
Closed this issue · 2 comments
ford153focus commented
Hello! I have next code:
#!/usr/bin/python3
from python_hosts import Hosts, HostsEntry
hosts = Hosts(path='/etc/hosts')
ip4_localhost_entries = list(filter(lambda host_entry: host_entry.address == '127.0.0.1' and 'mc.yandex.ru' in host_entry.names, hosts.entries))
ip6_localhost_entries = list(filter(lambda host_entry: host_entry.address == '::1' and 'mc.yandex.ru' in host_entry.names, hosts.entries))
if len(ip4_localhost_entries) == 0:
new_entry = HostsEntry(entry_type='ipv4', address='127.0.0.1', names=['mc.yandex.ru'])
hosts.add([new_entry])
if len(ip6_localhost_entries) == 0:
new_entry = HostsEntry(entry_type='ipv6', address='::1', names=['mc.yandex.ru'])
hosts.add([new_entry])
hosts.write()
I'm expecting to see in /etc/hosts
2 records: 127.0.0.1 mc.yandex.ru
and ::1 mc.yandex.ru
, but only first is appeared.
Did i something wrong?
jonhadfield commented
The issue is that you are adding two entries with the same name, so the second time you call hosts.add
, the library sees a duplicate and ignores it.
If you add force=True
when adding the second entry, the hosts file will contain: ::1 mc.yandex.ru
because you are forcing the removal of any existing entries with the same name before adding.
It doesn't really make sense to have multiple host file entries with the same name as i assume the OS will read the first only and ignore the second.
ford153focus commented
Many thanks!