zoukankan      html  css  js  c++  java
  • 以太网帧 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.
  • 相关阅读:
    chess「dp」
    e[树上主席树]
    d[贪心]
    神盐皇
    LA 8043. ACM-ICPC World Finals 2017 E. Need for Speed
    八数码问题
    UVa 679. Dropping Balls
    关于时间复杂度
    欧拉序列 (Euler Tour)
    莫队算法
  • 原文地址:https://www.cnblogs.com/hulian425/p/14044455.html
Copyright © 2011-2022 走看看