目的,过滤出websocket流中的binary包,并把data导出到文件中,用于分析流媒体。直接上代码:

import pyshark
import binascii

cap_file = 'test.cap'
output_file = 'ws_binary_output.bin'

capture = pyshark.FileCapture(
    cap_file,
    display_filter='tcp.port == 10094',
    use_json=True,
    include_raw=True
)

binary_data = bytearray()

for pkt in capture:
    if hasattr(pkt, 'websocket') and getattr(pkt.websocket, 'opcode', '') == '2':
        if hasattr(pkt, 'data') and hasattr(pkt.data, 'data'):
            hex_str = pkt.data.data.replace(':', '')
            try:
                raw_bytes = binascii.unhexlify(hex_str)
                binary_data += raw_bytes
                print(f"✔ Packet #{pkt.number} appended, {len(raw_bytes)} bytes")
            except Exception as e:
                print(f"[!] Error on packet #{pkt.number}: {e}")

with open(output_file, 'wb') as f:
    f.write(binary_data)

print(f"写入完成,总共 {len(binary_data)} 字节,输出文件: {output_file}")

Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐