From 89b1ca88467a9aa5d43179e3fb991cb2d1338aed Mon Sep 17 00:00:00 2001 From: Martin Zimmermann Date: Sun, 27 Oct 2013 13:58:50 +0100 Subject: [PATCH] doctests for utils.anonymize(remote_addr) --- isso/utils/__init__.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/isso/utils/__init__.py b/isso/utils/__init__.py index 9eb6cbb..cdcf6ad 100644 --- a/isso/utils/__init__.py +++ b/isso/utils/__init__.py @@ -11,14 +11,23 @@ import ipaddress def anonymize(remote_addr): + """ + Anonymize IPv4 and IPv6 :param remote_addr: to /24 (zero'd) + and /48 (zero'd). + + >>> anonymize(u'12.34.56.78') + u'12.34.56.0' + >>> anonymize(u'1234:5678:90ab:cdef:fedc:ba09:8765:4321') + u'1234:5678:90ab:0000:0000:0000:0000:0000' + """ try: ipv4 = ipaddress.IPv4Address(remote_addr) - return ''.join(ipv4.exploded.rsplit('.', 1)[0]) + '.' + '0' + return u''.join(ipv4.exploded.rsplit('.', 1)[0]) + '.' + '0' except ipaddress.AddressValueError: ipv6 = ipaddress.IPv6Address(remote_addr) if ipv6.ipv4_mapped is not None: return anonymize(ipv6.ipv4_mapped) - return ipv6.exploded.rsplit(':', 5)[0] + ':' + ':'.join(['0000']*3) + return u'' + ipv6.exploded.rsplit(':', 5)[0] + ':' + ':'.join(['0000']*5) def salt(value, s=u'\x082@t9*\x17\xad\xc1\x1c\xa5\x98'):