以太网帧 python 拆分

import socket
import struct
import textwrap
# struct模块来解决bytes和其他二进制数据类型的转换。

# Unpack ethernet frame
def ethernet_frame(data):
    dest_mac, src_mac, proto = struct.unpack('! 6s 6s H', data[:14])
    # 将前14位拆分成 6位, 6位, 2 位
    # 首位为!,即为大端模式标准对齐方式(network)
    # 默认为@,即使用本机的字符顺序(大端or小端)
    # h 代表C struct中的short类型,占2位
    return get_mac_addr(dest_mac), get_mac_addr(src_mac), socket.htons(protp), data[14:]


# socket.htons(x)
# Convert 16-bit positive integers from host to network byte order. 
# On machines where the host byte order is the same as network byte order, this is a no-op; 
# otherwise, it performs a 2-byte swap operation.
原文地址:https://www.cnblogs.com/hulian425/p/14044455.html