From 34ec2d1370d0066c47f37819b543ec8465332c12 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Wed, 13 Jan 2016 16:37:28 +0530 Subject: Export/Copy request as python code Press E followed by p to copy code to clipboard. The code uses the python requests module. Only GET methods are currently supported, but can easily be extended to POST etc. --- libmproxy/console/common.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'libmproxy/console/common.py') diff --git a/libmproxy/console/common.py b/libmproxy/console/common.py index 50dfa7dc..2ea77449 100644 --- a/libmproxy/console/common.py +++ b/libmproxy/console/common.py @@ -276,9 +276,13 @@ def copy_flow_format_data(part, scope, flow): raise ValueError("Unknown part: {}".format(part)) return data, False + def export_prompt(k, flow): if k == "c": copy_as_curl_command(flow) + elif k == "p": + copy_as_python_code(flow) + def copy_as_curl_command(flow): if flow.request.content is None or flow.request.content == CONTENT_MISSING: @@ -302,6 +306,31 @@ def copy_as_curl_command(flow): copy_to_clipboard_or_prompt(data) +def copy_as_python_code(flow): + if flow.request.content is None or flow.request.content == CONTENT_MISSING: + signals.status_message.send(message="Request content is missing") + return + + if flow.request.method != "GET": + signals.status_message.send(message="Currently, only GET methods are supported") + return + + data = ("import requests\n" + "headers = {%s}\n" + "url = '%s'\n" + "resp = requests.get(url, headers=headers)") + + headers = "\n" + for k, v in flow.request.headers.fields: + headers += " '%s': '%s',\n" % (k, v) + + full_url = flow.request.scheme + "://" + flow.request.host + flow.request.path + + data = data % (headers, full_url) + + copy_to_clipboard_or_prompt(data) + + def copy_to_clipboard_or_prompt(data): # pyperclip calls encode('utf-8') on data to be copied without checking. # if data are already encoded that way UnicodeDecodeError is thrown. @@ -326,6 +355,7 @@ def copy_to_clipboard_or_prompt(data): callback = save ) + def copy_flow(part, scope, flow, master, state): """ part: _c_ontent, _h_eaders+content, _u_rl -- cgit v1.2.3