用于生成专用IP地址的Python程序

时间:2015-08-04 05:54:01

标签: python

以下给出的程序是随机生成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])))

4 个答案:

答案 0 :(得分:2)

这是因为您正在检查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

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:0)

可以用这段代码完成吗?

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模块​​一起使用。

答案 3 :(得分:0)

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

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

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