_colon_colon_start = re.compile(br'::.*')
_colon_colon_end = re.compile(br'.*::$')
-def inet_aton(text):
+def inet_aton(text, ignore_scope=False):
"""Convert an IPv6 address in text form to binary form.
*text*, a ``text``, the IPv6 address in textual form.
+ *ignore_scope*, a ``bool``. If ``True``, a scope will be ignored.
+ If ``False``, the default, it is an error for a scope to be present.
+
Returns a ``bytes``.
"""
if not isinstance(text, bytes):
text = text.encode()
+ if ignore_scope:
+ parts = text.split(b'%')
+ l = len(parts)
+ if l == 2:
+ text = parts[0]
+ elif l > 2:
+ raise dns.exception.SyntaxError
+
if text == b'::':
text = b'0::'
#
self.assertFalse(dns.inet.is_multicast(t5))
self.assertTrue(dns.inet.is_multicast(t6))
+ def test_ignore_scope(self):
+ t1 = 'fe80::1%lo0'
+ t2 = 'fe80::1'
+ self.assertEqual(aton6(t1, True), aton6(t2))
+
+ def test_do_not_ignore_scope(self):
+ def bad():
+ t1 = 'fe80::1%lo0'
+ aton6(t1)
+ self.assertRaises(dns.exception.SyntaxError, bad)
+
+ def test_multiple_scopes_bad(self):
+ def bad():
+ t1 = 'fe80::1%lo0%lo1'
+ aton6(t1, True)
+ self.assertRaises(dns.exception.SyntaxError, bad)
+
if __name__ == '__main__':
unittest.main()