生成随机 IPv6 地址 [英] Generate random IPv6 address

查看:63
本文介绍了生成随机 IPv6 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,如果我想生成一个IP v6地址形式的随机字符串怎么办?

例如:ff80::220:16ff:fec9:1"、fe80::232:50ff:fec0:5"、fe20::150:560f:fec4:3"等.

有人可以帮我吗?

解决方案

单行解决方案:

str(ipaddress.IPv6Address(random.randint(0, 2**128-1)))

或者手工制作的地址(但是连续的零部分没有用双冒号代替):

':'.join('{:x}'.format(random.randint(0, 2**16 - 1)) for i in range(8))

In Python, what should I do if I want to generate a random string in the form of an IP v6 address?

For example: "ff80::220:16ff:fec9:1", "fe80::232:50ff:fec0:5", "fe20::150:560f:fec4:3" and so on.

Could somebody give me some help?

解决方案

One-line solution:

str(ipaddress.IPv6Address(random.randint(0, 2**128-1)))

Or handmade address (but consecutive sections of zeroes are not replaced with a double colon):

':'.join('{:x}'.format(random.randint(0, 2**16 - 1)) for i in range(8))

这篇关于生成随机 IPv6 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆