Python 打开并读取多个pcap文件
我正在尝试了解如何使用dpkt模块打开多个.pcap文件,并同时读取它们。经过长时间的谷歌搜索,我找到的示例只显示了如何打开和读取1.pcap文件 我尝试过使用多个for循环,并使用数组对文件进行zip()压缩,但没有效果。出现错误,ValueError:需要超过1个值才能解包。有什么建议吗?以下是我当前的python脚本:Python 打开并读取多个pcap文件,python,Python,我正在尝试了解如何使用dpkt模块打开多个.pcap文件,并同时读取它们。经过长时间的谷歌搜索,我找到的示例只显示了如何打开和读取1.pcap文件 我尝试过使用多个for循环,并使用数组对文件进行zip()压缩,但没有效果。出现错误,ValueError:需要超过1个值才能解包。有什么建议吗?以下是我当前的python脚本: import dpkt, socket, glob, pcap, os files = [open(f) for f in glob.glob('*.pcap
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')
输出:
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')
zip()
for ts, data in zip(abc, fgh):
//...
通过首先列出列表,您仅给出了要迭代的对象的zip()
,该对象恰好包含可以迭代的对象。您实际上非常接近。您只需解压缩要传递给zip()
的序列