我想了解如何使用dpkt模塊打開多個 .pcap文件並在相同的時間讀取它們。經過大量的搜索和長時間的搜索後,我只能找到的示例顯示瞭如何打開並閱讀1個.pcap文件。打開並讀取多個pcap文件
我試過使用多於1的循環,並使用數組壓縮()文件,但無濟於事。有一個錯誤,ValueError:需要多個值才能解包。有什麼建議麼?這是我目前的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')