【发布时间】:2018-03-31 02:38:00
【问题描述】:
我正在尝试在 Python3 中创建示例 UDP 数据包头:
# "encoding"
header = bytearray()
ip = '192.168.1.1'
ip_bytes = bytes(map(int, ip.split('.')))
header.extend(ip_bytes)
port = 5555
header.extend(port.to_bytes(2, 'big'))
print(header)
print()
# "decoding"
destip = header[:4]
ips = ""
for i in destip:
byt = int.from_bytes(destip[i:i+1], 'big')
ips += str(byt) + "."
ips = ips[:len(ips)-1]
print(ips)
输出是:
bytearray(b'\xc0\xa8\x01\x01\x15\xb3')
bytearray(b'\xc0\xa8\x01\x01')
0.0.168.168
我想要的是第二行是:
192.168.1.1
有人知道我哪里出错了吗?
【问题讨论】:
-
@thatrockbottomprogrammer 我正在努力将 ip BACK 转换为字符串
标签: python-3.x networking udp ip