把 :: 展开
expand 把缩写的 IPv6 展开成八组、每组四位:
def expand(addr):
if "::" in addr:
head, tail = addr.split("::")
h = head.split(":") if head else []
t = tail.split(":") if tail else []
groups = h + ["0"] * (8 - len(h) - len(t)) + t
else:
groups = addr.split(":")
return ":".join(g.zfill(4) for g in groups)
e = expand("2001:db8::1")
print(e + "/" + str(len(e.split(":"))))
全部评论