python - IP地址转换使用Python

标签 python

我想将从文件中读取的十进制格式 (192.168.65.72) 的 IP 地址转换为二进制格式的 IP 地址 {110000001010100001000001010001000}。我正在从文件中读取十进制格式的 IP 地址。找到代码片段下面。

/*contains 192.168.65.72*/
filter = open("filter.txt", "r")

for line in filter:
    bytePattern = "([01]?\d\d?|2[0-4]\d|25[0-5])"
    regObj = re.compile("\.".join([bytePattern]*4))
    for match in regObj.finditer(line):
        m1,m2,m3,m4 = match.groups()
        print "%s %s %s %s" %(m1, m2, m3, m4)

我想将 m1、m2、m3 和 m4 分别转换成一个 8 位二进制值。我想不出办法做到这一点。我是 python 的新手。任何帮助将不胜感激。

干杯。

最佳答案

''.join([ bin(int(x))[2:].rjust(8,'0') for x in '123.123.123.123'.split('.')])

关于python - IP地址转换使用Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3465099/

相关文章:

python - 将 pytorch 转换为 coreml 以进行元素最大操作

python - 更改 Abaqus 中的默认元素类型

python - Django模板问题: how to output just the text if the variable has html in it?

python - Pandas :将系列的数据类型更改为字符串

Python 3 : maketime()

python - 区间类 - Python

python - 从 python 脚本运行交互模式

python - PyQt:如何在QGraphicsView中设置滚动条和显示区域大小策略

python - Pandas 时间序列图设置 x 轴主要和次要刻度和标签

Python lmfit 约束 : a < b < c