From 3374be9f4b1dbeba3e9abbea7a447c957dfeda51 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Mon, 8 Feb 2016 20:07:38 +0530 Subject: Move exporters to a separate file --- libmproxy/flow_export.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 libmproxy/flow_export.py (limited to 'libmproxy/flow_export.py') diff --git a/libmproxy/flow_export.py b/libmproxy/flow_export.py new file mode 100644 index 00000000..584b3be4 --- /dev/null +++ b/libmproxy/flow_export.py @@ -0,0 +1,70 @@ +import urllib +import netlib.http + + +def curl_command(flow): + data = "curl " + + for k, v in flow.request.headers.fields: + data += "-H '%s:%s' " % (k, v) + + if flow.request.method != "GET": + data += "-X %s " % flow.request.method + + full_url = flow.request.scheme + "://" + flow.request.host + flow.request.path + data += "'%s'" % full_url + + if flow.request.content: + data += " --data-binary '%s'" % flow.request.content + + return data + + +def python_code(flow): + code = """import requests + +url = '{url}' +{headers}{params}{data} +response = requests.request( + method='{method}', + url=url,{args} +) + +print(response.text)""" + + components = map(lambda x: urllib.quote(x, safe=""), flow.request.path_components) + url = flow.request.scheme + "://" + flow.request.host + "/" + "/".join(components) + + args = "" + headers = "" + if flow.request.headers: + lines = [" '%s': '%s',\n" % (k, v) for k, v in flow.request.headers.fields] + headers += "\nheaders = {\n%s}\n" % "".join(lines) + args += "\n headers=headers," + + params = "" + if flow.request.query: + lines = [" '%s': '%s',\n" % (k, v) for k, v in flow.request.query] + params = "\nparams = {\n%s}\n" % "".join(lines) + args += "\n params=params," + + data = "" + if flow.request.body: + data = "\ndata = '''%s'''\n" % flow.request.body + args += "\n data=data," + + code = code.format( + url=url, + headers=headers, + params=params, + data=data, + method=flow.request.method, + args=args, + ) + + return code + + +def raw_request(flow): + data = netlib.http.http1.assemble_request(flow.request) + return data -- cgit v1.2.3 From 33c514e2a7d0c2458cf7cbd7bcf1ee6762bd24fa Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Wed, 10 Feb 2016 14:13:32 +0530 Subject: Use textwrap.dedent in flow_export.python_code too --- libmproxy/flow_export.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'libmproxy/flow_export.py') diff --git a/libmproxy/flow_export.py b/libmproxy/flow_export.py index 584b3be4..52145516 100644 --- a/libmproxy/flow_export.py +++ b/libmproxy/flow_export.py @@ -1,5 +1,6 @@ import urllib import netlib.http +from textwrap import dedent def curl_command(flow): @@ -21,16 +22,18 @@ def curl_command(flow): def python_code(flow): - code = """import requests - -url = '{url}' -{headers}{params}{data} -response = requests.request( - method='{method}', - url=url,{args} -) - -print(response.text)""" + code = dedent(""" + import requests + + url = '{url}' + {headers}{params}{data} + response = requests.request( + method='{method}', + url=url,{args} + ) + + print(response.text) + """).strip() components = map(lambda x: urllib.quote(x, safe=""), flow.request.path_components) url = flow.request.scheme + "://" + flow.request.host + "/" + "/".join(components) -- cgit v1.2.3