Convert an IPv6 address in Compact Representation Notation (RFC 1924) to printable representation ;-) Returns None on error.
(addr)
| 476 | |
| 477 | |
| 478 | def in6_ctop(addr): |
| 479 | # type: (str) -> Optional[str] |
| 480 | """ |
| 481 | Convert an IPv6 address in Compact Representation Notation |
| 482 | (RFC 1924) to printable representation ;-) |
| 483 | Returns None on error. |
| 484 | """ |
| 485 | if len(addr) != 20 or not reduce(lambda x, y: x and y, |
| 486 | [x in _rfc1924map for x in addr]): |
| 487 | return None |
| 488 | i = 0 |
| 489 | for c in addr: |
| 490 | j = _rfc1924map.index(c) |
| 491 | i = 85 * i + j |
| 492 | res = [] |
| 493 | for j in range(4): |
| 494 | res.append(struct.pack("!I", i % 2**32)) |
| 495 | i = i // (2**32) |
| 496 | res.reverse() |
| 497 | return inet_ntop(socket.AF_INET6, b"".join(res)) |
| 498 | |
| 499 | |
| 500 | def in6_ptoc(addr): |