Created
March 29, 2011 14:49
-
-
Save craSH/892479 to your computer and use it in GitHub Desktop.
Parse a HAR (HTTP Archive) and return URLs which resulted in a given HTTP response code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Parse a HAR (HTTP Archive) and return URLs which resulted in a given HTTP response code | |
HAR Spec: http://groups.google.com/group/http-archive-specification/web/har-1-2-spec | |
Copyleft 2010 Ian Gallagher <[email protected]> | |
Example usage: ./har_response_urls.py foo.har 404 | |
""" | |
import json | |
if '__main__' == __name__: | |
import sys | |
if len(sys.argv) < 3: | |
print "Usage: %s <har_file> <HTTP response code>" % sys.argv[0] | |
sys.exit(1) | |
har_file = sys.argv[1] | |
response_code = int(sys.argv[2]) | |
# Read HAR archive (skip over binary header if present - Fiddler2 exports contain this) | |
har_data = open(har_file, 'rb').read() | |
skip = 3 if '\xef\xbb\xbf' == har_data[:3] else 0 | |
har = json.loads(har_data[skip:]) | |
matching_entries = filter(lambda x: response_code == x['response']['status'], har['log']['entries']) | |
matching_urls = set(map(lambda x: x['request']['url'], matching_entries)) | |
print >>sys.stderr, "URLs which resulted in an HTTP %d response:" % response_code | |
for url in matching_urls: | |
print url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @sruti842 ! Ping on a nice old gist :)
You can use python's
in range(..)
as the lambda argument here, like so:That should return anything with status codes between 400 and 499, inclusive. Keep in mind that the upper bound to range() is excluded, so you must add one in order to have that behavior. Adjust as desired for your coding style. Here's a little example that you can just run in python or ipython to see the pattern in action: