Skip to content

Instantly share code, notes, and snippets.

@huyphan
Created May 31, 2017 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huyphan/0ea6bb022b161ec0ed20323bf9afad22 to your computer and use it in GitHub Desktop.
Save huyphan/0ea6bb022b161ec0ed20323bf9afad22 to your computer and use it in GitHub Desktop.
'''
Generating Python script that replays HTTP requests captured by mitmproxy/mitmdump.
Usage:
$ mitmdump -s /tmp/replay_script_generator.py 2>&1 >/dev/null
'''
import pprint
import sys
pp = pprint.PrettyPrinter(indent=4)
REPLAY_SCRIPT = """
method="{method}"
url="{url}"
headers={headers}
cookies={cookies}
data={data}
requests.request(method, url, headers=headers, cookies=cookies, data=data)
"""
def response(flow):
headers = flow.request.headers
headers_dict = {}
for header in headers:
if 'cookie' != header.lower():
headers_dict[header] = headers[header]
cookies = dict(flow.request.cookies)
data = flow.request.content
params = {
'method': flow.request.method,
'url': flow.request.url,
'headers': pp.pformat(headers_dict),
'cookies': pp.pformat(cookies),
'data': repr(data)
}
print(REPLAY_SCRIPT.format(**params), file=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment