2014-11-17 51 views
0

目前,我從PCAP文件解析HTTP頭,像這樣:如何分析PCAP頭在Python,同時保持頭字段順序

f = file(sys.argv[1],"rb") # pass in pcap file as argument to script 
fout = open("path to header output file", "a") 
pcap = dpkt.pcap.Reader(f) 

# master holds string to write 
master = "" 
print "Working ..." 
for ts, buf in pcap: 
    l2 = dpkt.ethernet.Ethernet(buf) 
    if l2.type == 2048: #only for IP (ip id 2048), no ARP 
    l3=l2.data 
    if l3.p == dpkt.ip.IP_PROTO_TCP: #IP TCP 
     l4=l3.data 
     if l4.dport==80 and len(l4.data)>0: 
     try: 
      http=dpkt.http.Request(l4.data) 
      dict_headers = http.headers 
      http_method = http.method 
      http_uri = http.uri 
      http_body = http.body 
      http_version = http.version 

      # this is for first line, method + uri, e.g. GET URI 
      master += unicode(http_method + ' ' + http_uri + ' ' + 'HTTP/' + http_version + '\n','utf-8') 

      for key,val in dict_headers.iteritems(): 
      master += unicode(key + ': ' + val + '\n', 'utf-8') 

      master += '\n' 
     except: 
      master += unicode(l4.data, 'utf-8') 
      continue 

# perform writing and closing of files, etc 

的問題是,dpkt商店HTTP字典中的字段(http.headers) ,這是無序的。我需要保留字段的順序。有沒有辦法解決?

回答

1

有兩個選項:

  1. 可以改變dpkt的代碼中使用OrderedDict,而不是普通的字典(沒有嘗試)。 OrderedDict保留了插入的順序。

  2. 自己解析HTTP請求,每個標頭值以\ x0d \ x0a結尾。每個頭名有「:」在它的末端,所以你可以使用分裂,使標題的列表(有序)這樣說:

    l5 = l4.data 
    headers_and_content = l5[l5.index('\x0d\x0a')+2:l5.index('\x0d\x0a\x0d\x0a')].split('\x0d\x0a') 
    ordered_headers = [] 
    for item in headers_and_content: 
        ordered_headers.append(item[:item.index(':')]) 
    
相關問題