我试图找出如何使用 dpkt 模块打开多个.pcap 文件并同时读取它们。经过大量的谷歌搜索和长时间的搜索,我设法找到的示例仅显示了如何打开和读取 1 个 .pcap 文件。
我尝试使用超过 1 个 for 循环,并使用数组 zip() 文件,但无济于事。有一个错误,ValueError: need more than 1 value to unpack。有什么建议么?这是我当前的python脚本:
import dpkt, socket, glob, pcap, os
files = [open(f) for f in glob.glob('*.pcap')]
abc = dpkt.pcap.Reader(file("abc.pcap", "rb"))
fgh = dpkt.pcap.Reader(file("fgh.pcap", "rb"))
print files
print "\r\n"
List = [abc, fgh]
for ts, data in zip(List):
eth = dpkt.ethernet.Ethernet(data)
ip = eth.data
tcp = ip.data
src = socket.inet_ntoa(ip.src)
dst = socket.inet_ntoa(ip.dst)
if tcp.dport == 80 and len(tcp.data) > 0:
http = dpkt.http.Request(tcp.data)
print "-------------------"
print "HTTP Request /", http.version
print "-------------------"
print "Type: ", http.method
print "URI: ", http.uri
print "User-Agent: ", http.headers ['user-agent']
print "Source: ", src
print "Destination: ", dst
print "\r\n"
编辑://
嘿,谢谢所有的建议。为了简化这个过程,我现在修改了我的代码以打开 .txt 文件。我的代码如下所示。输出中没有显示错误,但是当我打印输出时,如何去掉换行符 '\n'、方括号和单引号?
代码:
import glob
fileList = [glob.glob('*.txt')]
for files in fileList:
print "Files present:",files
print ""
a = open("1.txt", 'r')
b = open("2.txt", 'r')
List = [a,b]
for line in zip(*List):
print line
输出:
>Files present: ['2.txt', '1.txt']
>
>('This is content from the FIRST .txt file\n', 'This is content from the SECOND .txt file\n')
>('\n', '\n')
>('Protocol: Testing\n', 'Protocol: PCAP\n')
>('Version: 1.0\n', 'Version: 2.0\n')