Skip to main content
 首页 » 编程设计

python之如何打印PCAP文件中的所有目标端口和源端口

2025年05月04日14lhb25

import pyshark 
pkts = pyshark.FileCapture("test.pcap") 
 
 
for p in pkts: 
      print 

我正在尝试打印 PCAP 文件中的所有目标端口和源端口。我该怎么做?

请您参考如下方法:

import pyshark 
 
pkts = pyshark.FileCapture('cap.pcap') 
 
for p in pkts: 
    if hasattr(p, 'tcp'): 
        print(p.tcp.srcport + ' -- ' + p.tcp.dstport) 
    if hasattr(p, 'udp'): 
        print(p.udp.srcport + ' -- ' + p.udp.dstport)