gpt4 book ai didi

python - 从 pypcapfile 中的 TCP header 获取 ByteArray

转载 作者:可可西里 更新时间:2023-11-01 02:53:50 25 4
gpt4 key购买 nike

我想在不使用 PCAPLib 自己的数据结构的情况下单独解析 TCP 数据包。因此,我需要获取 TCP header 的字节数组。

from pcapfile import savefile

capfile = open('delta_capture.pcap')
sf = savefile.load_savefile(capfile)

for packet in sf.packets:
print packet.timestamp
print packet.packet
print packet.header # Returns a library object, I need the bytearray instead, as I want to use my own data structure and parse.

capfile.close()

我尝试调试和检查对象结构,但看不到任何在 TCP header 中存储实际字节的对象。

变量“packet”的调试器结果截图:

Screenshot for debugger result for the variable "packet"

甚至可以在这个库中这样做吗?

最佳答案

header 的 bytearray 不可直接访问。解析包头中的各个字段,得到整个包:

for packet in sf.packets:
print(packet.timestamp)
print(packet.packet)

# show header fields
print(packet.header.contents.magic) # file magic number
print(packet.header.contents.major) # major version number
print(packet.header.contents.minor) # minor version number
print(packet.header.contents.tz_off) # timezone offset
print(packet.header.contents.ts_acc) # timestamp accuracy
print(packet.header.contents.snaplen) # snapshot length
print(packet.header.contents.ll_type) # link layer header type
print(packet.header.contents.byteorder) # byte order specifier
print(packet.header.contents.ns_resolution) # nanosecond resolution

# show entire packet
print(packet.raw())

关于python - 从 pypcapfile 中的 TCP header 获取 ByteArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42568795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com