【问题标题】:Python Program to generate the Private IP Addresses生成私有 IP 地址的 Python 程序
【发布时间】:2015-10-26 09:45:36
【问题描述】:

下面给出的程序是随机生成N个私有IP地址。

如果 x1 = 172,x2 的值应该是从 16 到 31...但它生成的值是从 0 到 255...有人可以看看并告诉我可能是什么错误吗?

代码:

import random

n = int(raw_input("How many IP addresses need to be generated \n"))

x1 = random.choice(["10","172","192"])


for i in range (0,n):

    if (x1 == 10 ):

        x2 = random.randint(0,255)

        x3 = random.randint(0,255)

        x4 = random.randint(0,255)

        print ".".join(map(str,([x1,x2,x3,x4])))



    elif (x1 == 172):

        x2 = random.randint(16,31)

        x3 = random.randint(0,255)

        x4 = random.randint(0,255)

        print ".".join(map(str,([x1,x2,x3,x4])))


    else:

        x2 = random.randint(0,255)

        x3 = random.randint(0,255)

        x4 = random.randint(0,255)

        print ".".join(map(str,([x1,x2,x3,x4])))

【问题讨论】:

标签: python


【解决方案1】:

那是因为你正在检查int with string

即)

1=="1"
Out[10]: False

修改:

import random
n = int(raw_input("How many IP addresses need to be generated \n"))
x1 = random.choice(["172","192","10"])    
for i in range (0,n):
    if (int(x1) == 10 ):
        x2 = random.randint(0,255)        
        x3 = random.randint(0,255)
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

    elif (int(x1) == 172):
        x2 = random.randint(16,31)            
        x3 = random.randint(0,255)
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

    else:    
        x2 = random.randint(0,255)
        x3 = random.randint(0,255)        
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

编辑:

import random
n = int(raw_input("How many IP addresses need to be generated \n"))
x1 = random.choice([172,192,10])    
for i in range (0,n):
    if (int(x1) == 10 ):
        x2 = random.randint(0,255)        
        x3 = random.randint(0,255)
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

    elif (int(x1) == 172):
        x2 = random.randint(16,31)            
        x3 = random.randint(0,255)
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

    else:    
        x2 = random.randint(0,255)
        x3 = random.randint(0,255)        
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

edit2:

由于if and else 或做同样的事情,你可以将它们合并为else

import random
n = int(raw_input("How many IP addresses need to be generated \n"))
x1 = random.choice([172,192,10])    
for i in range (0,n):
    if (int(x1) == 172):
        x2 = random.randint(16,31)            
        x3 = random.randint(0,255)
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

    else:    
        x2 = random.randint(0,255)
        x3 = random.randint(0,255)        
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

输出:

How many IP addresses need to be generated 
20
10.88.254.205
10.205.49.201
10.67.147.81
10.10.75.63
10.136.166.197
10.241.237.2
10.33.204.114
10.10.190.132
10.11.72.207
10.24.178.32
10.215.156.125
10.75.79.15
10.47.159.174
10.177.12.191
10.96.189.105
10.141.216.118
10.99.138.176
10.92.138.176
10.81.147.16
10.246.147.4

edit3:

我个人认为x1 that is ip first segment should change randomly when looping

因此编辑代码:

import random
n = int(raw_input("How many IP addresses need to be generated \n"))

for i in range (0,n):
    x1 = random.choice([172,192,10]) #moved inside loop
    if (int(x1) == 172):
        x2 = random.randint(16,31)            
        x3 = random.randint(0,255)
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

    else:    
        x2 = random.randint(0,255)
        x3 = random.randint(0,255)        
        x4 = random.randint(0,255)
        print ".".join(map(str,([x1,x2,x3,x4])))

输出:

How many IP addresses need to be generated 
20
192.178.231.68
192.253.233.190
192.114.158.193
192.196.30.127
172.22.44.3
172.23.180.6
10.69.55.105
10.105.63.195
172.21.0.195
192.83.125.135
10.36.196.137
10.8.251.102
192.38.130.4
172.22.21.131
10.204.243.231
192.136.121.203
172.30.89.149
192.40.178.100
192.155.127.75
172.23.97.228

【讨论】:

  • @vignesh.it 应该是 x1 = random.choice([10,172,192]) 而不是 x1 = random.choice(["10","172","192"]) ?
  • @Maverick 是的,你可以这样做
  • ,当我认为它开始工作时,我给了 x1 = random.choice([10,172,192])
  • @Maverick 是的,因为您正在检查int against int it would work :)
  • @Maverick 立即查看答案 :)
【解决方案2】:

当设置 x1 时,您使用引号使其成为字符串。删除引号,它将是一个数字。当您将其与数值进行比较时,这一点很重要。

x1 = random.choice([10,172,192])

然后比较 elif (x1 == 172) 将按预期工作。

【讨论】:

    【解决方案3】:

    可以用这段代码完成吗?

    from random import randint
    def randomPrivateIPaddress():
        network_type = randint(0, 2)
        if network_type == 0:
            return "10.%d.%d.%d" % (randint(0, 255), randint(0, 255), randint(0, 255))
        elif network_type == 1:
            return "172.%d.%d.%d" % (randint(16, 31), randint(0, 255), randint(0, 255))
        else:
            return "192.168.%d.%d" % (randint(0,255), randint(0,255))
    

    然后,您可以将这些字符串与 ipaddress 模块一起使用。

    【讨论】:

      【解决方案4】:

      这里的许多人都提供了很好的解决方案来处理众所周知的私有 IP 地址。我所说的“众所周知”是指:

      192.168.xxx.xxx
      172.16.xxx.xxx - 172.16.31.255.255
      10.xxx.xxx.xxx
      

      但是。我确实想澄清还有许多其他“保留”的 IP 地址范围不用于基于IANA documentation 的公共用途。这些都可以被认为是在“私人”空间中。 最值得注意的是当今广泛使用的 localhost 范围 (127.xxx.xxx.xxx) 和多播地址 (240.0.0.0/4)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-14
        • 1970-01-01
        • 2010-09-10
        • 1970-01-01
        • 2020-01-10
        • 2012-03-03
        • 2017-11-27
        • 1970-01-01
        相关资源
        最近更新 更多