2012-08-30 149 views
0

我想了解如何使用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') 

回答

0

zip()需要每一件事情来遍历作为独立参数。

for ts, data in zip(abc, fgh): 
    //... 

通过使榜第一,你只给上一点zip()遍历,那东西正好包含东西可以遍历。

0

你其实关闭。你只需要解压你传递给zip()的序列。

for ts, data in zip(*List):