【问题标题】:What's the quickest way to cut the first 20 bytes out of a byte string in Python?在 Python 中从字节字符串中删除前 20 个字节的最快方法是什么?
【发布时间】:2016-04-18 03:40:15
【问题描述】:

我正在使用 TCP 协议将数据包从发送方传递到接收方。我需要能够将标题与填充我的“数据包”的内容分开。

我的标题是这样包装的

tcp_header = pack('!HHLLBBH', tcp_source, tcp_dest, tcp_seq, tcp_ack_seq, tcp_offset_res, tcp_flags,
                          tcp_window) + pack('H', tcp_check) + pack('!H', tcp_urg_ptr)

那么我的包是这样构造的

packet = tcp_header + user_data

我是这样解压的

(tcp_source, tcp_dest, tcp_seq, tcp_ack_seq, tcp_offset_res, tcp_flags,\
             tcp_window, tcp_check, tcp_urg_ptr) = unpack('!HHLLBBHHH', sent_packet)

但它只有在我单独发送标头时才有效。解包前如何从头部拆分内容?

谢谢

【问题讨论】:

    标签: python tcp pack unpack


    【解决方案1】:

    你要找的函数struct.unpack_from

    struct.unpack_from('!HHLLBBHHH', sent_packet)
    

    【讨论】:

    • 谢谢!。结合 struct.calcsize 让我走上正轨。
    猜你喜欢
    • 2017-05-19
    • 1970-01-01
    • 2011-11-14
    • 2020-12-05
    • 2019-05-10
    • 2010-09-06
    • 2016-08-11
    • 2011-03-14
    • 1970-01-01
    相关资源
    最近更新 更多