Anonymize IPv4 and IPv6 :param remote_addr: to /24 (zero'd) and /48 (zero'd).
(remote_addr)
| 14 | |
| 15 | |
| 16 | def anonymize(remote_addr): |
| 17 | """ |
| 18 | Anonymize IPv4 and IPv6 :param remote_addr: to /24 (zero'd) |
| 19 | and /48 (zero'd). |
| 20 | |
| 21 | """ |
| 22 | if isinstance(remote_addr, bytes): |
| 23 | remote_addr = remote_addr.decode("ascii", "ignore") |
| 24 | try: |
| 25 | ipv4 = ipaddress.IPv4Address(remote_addr) |
| 26 | return "".join(ipv4.exploded.rsplit(".", 1)[0]) + "." + "0" |
| 27 | except ipaddress.AddressValueError: |
| 28 | try: |
| 29 | ipv6 = ipaddress.IPv6Address(remote_addr) |
| 30 | if ipv6.ipv4_mapped is not None: |
| 31 | return anonymize(str(ipv6.ipv4_mapped)) |
| 32 | return "" + ipv6.exploded.rsplit(":", 5)[0] + ":" + ":".join(["0000"] * 5) |
| 33 | except ipaddress.AddressValueError: |
| 34 | return "0.0.0.0" |
| 35 | |
| 36 | |
| 37 | class Bloomfilter: |