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 ++++++++++++++++++++++++++++++ libmproxy/console/flowlist.py | 1 + libmproxy/console/flowview.py | 3 ++- 3 files changed, 33 insertions(+), 1 deletion(-) (limited to 'libmproxy') 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 diff --git a/libmproxy/console/flowlist.py b/libmproxy/console/flowlist.py index 6c0ff3d0..b52c0fea 100644 --- a/libmproxy/console/flowlist.py +++ b/libmproxy/console/flowlist.py @@ -261,6 +261,7 @@ class ConnectionItem(urwid.WidgetWrap): prompt = "Export", keys = ( ("as curl command", "c"), + ("as python code", "p"), ), callback = common.export_prompt, args = (self.flow,) diff --git a/libmproxy/console/flowview.py b/libmproxy/console/flowview.py index 95d52c98..3a745262 100644 --- a/libmproxy/console/flowview.py +++ b/libmproxy/console/flowview.py @@ -27,7 +27,7 @@ def _mkhelp(): ("b", "save request/response body"), ("D", "duplicate flow"), ("d", "delete flow"), - ("E", "export"), + ("E", "export"), ("e", "edit request/response"), ("f", "load full body data"), ("m", "change body display mode for this entity"), @@ -581,6 +581,7 @@ class FlowView(tabs.Tabs): prompt = "Export", keys = ( ("as curl command", "c"), + ("as python code", "p"), ), callback = common.export_prompt, args = (self.flow,) -- cgit v1.2.3