Python randomly generated IP address as string

前端 未结 9 2075
遥遥无期
遥遥无期 2020-12-29 23:14

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

For example: \"10.0.1.1\", \"10.0.3.14\",

9条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 23:39

    It may be too obvious but if you need random IPs within a range you can use this:

    import random
    
    for x in xrange(1,100):
      ip = "192.168."
      ip += ".".join(map(str, (random.randint(0, 255) 
                              for _ in range(2))))
    
      print ip
    

提交回复
热议问题