2012-08-30 6 views
0

dpkt 모듈을 사용하여 .pcap 파일을 여러 개 열어 동일한 시간으로 읽는 방법을 찾으려고합니다. 많은 인터넷 검색 및 많은 오랜 시간 후에, 내가 찾을 수있는 예제는 오직 .pcap 파일을 열고 읽는 방법을 보여줍니다.여러 pcap 파일 열기 및 읽기

하나 이상의 for 루프를 사용하고 배열을 사용하여 파일을 zip()했으나 아무런 문제가 없습니다. ValueError : 포장을 풀려면 1 개 이상의 값이 필요합니다. 어떤 제안?

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() 별도의 인수로 반복하는 각 일을합니다. 첫 번째 목록을 만들고, 당신은 단지 반복하는 일에 zip()을주고있다하여

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

는, 그 것은 단지 이상 반복 할 수있는 일을 포함 발생합니다.

0

실제로는 매우입니다. 전달할 시퀀스를 zip()으로 풀면됩니다.

for ts, data in zip(*List):