【问题标题】:Python Convert Binary String to IP Address in Dotted NotationPython以点分表示法将二进制字符串转换为IP地址
【发布时间】:2016-05-13 00:38:22
【问题描述】:

所以我试图用一些二进制字符串读入一个文件,即: 10000010 00000000 0000**** ********。该脚本会将 * 转换为 0 和 1,因此会有两个如下所示的二进制字符串:

10000010 00000000 00000000 00000000 和 10000010 00000000 00001111 11111111。

然后脚本会将它们转换为 ip 地址,所以在这个例子中,我的脚本应该返回 130.0.0.0 和 130.0.15.255

这是我目前的代码:

def main():
    text=open('filename', 'r').readlines()
    for line in text:
        words = line.split(" ")
        words_2=list(words)
        for char in words:
            low_range=char.replace('*','0')
            conversion=str(int(low_range, 2))
            decimal='.'.join(map(str,conversion))
            print(decimal)
        for char in words_2:
            high_range=char.replace('*','1')
            conversion_2=str(int(high_range, 2))
            decimal='.'.join(map(str,conversion_2))
            print(decimal)
main()

当我运行我的代码时,它会打印出来:

1.3.0  
0  
0  
0  
1.3.0  
0  
6.3  
2.5.5  
1.3.0  
0  
6.4  
0  
1.3.0  
0  
9.5  
2.5.5  
1.3.0  
0  
1.2.8  
0  
1.3.0  
0  
1.9.1  
2.5.5  
1.3.0  
0  
1.3.0  
0  
1.9.2  
0  
1.3.0  
0  
2.5.5  
2.5.5 

当我真的想打印出来时:

130.0.0.0  
130.0.63.255  
130.0.64.0  
130.0.95.255  
130.0.128.0  
130.0.191.255  
130.0.192.0  
130.0.255.255  

谁能帮助解释我做错了什么?

【问题讨论】:

    标签: python networking binary ip


    【解决方案1】:

    你正在加入字节的十进制表示的字母,而你应该加入字节本身。

    decimal='.'.join(map(str,conversion))
    

    您还可以在自己的行上打印 ip 的每个字节

    print(decimal)
    

    这是我编写循环的方式:

    for line in text:
        words = line.split(" ")
    
        for bit in '01':        
            ip = []
            for word in words:
                byte=word.replace('*', bit)
                ip.append(str(int(byte, 2)))
            print '.'.join(ip)
    

    【讨论】:

    • 非常感谢!完美运行。假设我想在 0 的 ip 地址和 1 的 ip 地址之间设置一个查找范围,例如在 130.0.0.0-130.0.63.255 之间。这会像添加“for i in range(ip)”循环一样简单吗?
    【解决方案2】:

    您可以使用简单的格式:

    "%d.%d.%d.%d" % (0b10000010, 0b00000000, 0b00000000, 0b00000000)
    # '130.0.0.0'
    

    要从字符串中读取二进制数(比如"10000010"),请使用:

    int("10000010", 2)
    # 130
    

    如果你想使用 IP 地址,我建议使用 ipaddress:

    >>> import ipaddress
    >>> ipaddress.IPv4Address("%d.%d.%d.%d" % (0b10000010, 0b00000000, 0b00000000, 0b00000000))
    IPv4Address('130.0.0.0')
    

    但是,它在 python 2.x 下不可用

    【讨论】:

      【解决方案3】:

      您的示例将输入按空格拆分为变量words。然后遍历单词,将它们转换为 int 并返回到字符串到变量 conversion。这一切都说得通。

      问题出在下面一行:

      decimal='.'.join(map(str,conversion))
      

      第一次执行时,值为'130'map 调用是不必要的,它只是将 conversion 转换为字符串列表:['1', '3', '0']。然后将字符串与. 连接在一起,这样就得到了在输出中看到的1.3.0。请注意,即使删除 map 也会得到相同的结果,因为 join 只会遍历字符串中的字符。

      无需遍历字符,只需使用int 转换每个单词,就像您正在做的那样并将它们收集到一个列表中。然后使用map 或列表理解将它们转换为字符串,最后使用. 将它们连接在一起。

      这是一个简短的例子:

      with open('test.txt') as text:
          for line in text:
              print '.'.join(str(int(x, 2)) for x in line.split())
      

      【讨论】:

        猜你喜欢
        • 2011-02-13
        • 1970-01-01
        • 2013-11-13
        • 2011-05-04
        • 2020-10-22
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多