首页 > 编程语言 > 详细

以太网帧 python 拆分

时间:2020-11-26 22:27:41      阅读:38      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 

 

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.

 

以太网帧 python 拆分

原文:https://www.cnblogs.com/hulian425/p/14044455.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!