Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: messy
Fetching contributors…

Cannot retrieve contributors at this time

executable file 57 lines (46 sloc) 1.373 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#!/usr/bin/python

'''
Main program that converts pcaps to HAR's.
'''

import pcap
import os
import optparse
import logging
import sys
import http
import httpsession
import har
import json

# get cmdline args/options
parser = optparse.OptionParser(usage='usage: %prog inputfile outputfile [options]')
#parser.add_option('-d', '--directory', dest="dirname", default='flowdata', help="Directory to write flow files to.")
options, args = parser.parse_args()

# setup logs
logging.basicConfig(filename='pcap2har.log', level=logging.DEBUG)

# get filenames, or bail out with usage error
if len(args) == 2:
    inputfile, outputfile = args[0:2]
else:
    parser.print_help()
    sys.exit()

logging.info("Processing %s", inputfile)
flows = pcap.TCPFlowsFromFile(inputfile)

# generate HTTP Flows
httpflows = []
flow_count = 0
for f in flows.flowdict.itervalues():
    try:
        httpflows.append(http.Flow(f))
        flow_count += 1
    except http.Error as error:
        logging.warning(error)

# put all message pairs in one list
def combine_pairs(pairs, flow):
    return pairs + flow.pairs
pairs = reduce(combine_pairs, httpflows, [])

logging.info("Flow=%d HTTP=%d" % (flow_count,len(pairs)))
# parse HAR stuff
session = httpsession.HTTPSession(pairs)

with open(outputfile, 'w') as f:
    json.dump(session, f, cls=har.JsonReprEncoder, indent=2, encoding='utf8')

pass
Something went wrong with that request. Please try again.