mitmproxy/mitmproxy/flow_export.py

94 lines
2.4 KiB
Python
Raw Normal View History

2016-03-03 01:11:26 +00:00
import json
2016-02-08 14:37:38 +00:00
import urllib
from textwrap import dedent
2016-02-08 14:37:38 +00:00
import netlib.http
2016-03-03 01:11:26 +00:00
from netlib.utils import parse_content_type
2016-02-08 14:37:38 +00:00
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 = dedent("""
import requests
url = '{url}'
{headers}{params}{data}
response = requests.request(
method='{method}',
url=url,{args}
)
print(response.text)
""").strip()
2016-02-08 14:37:38 +00:00
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:
json_obj = is_json(flow.request.headers, flow.request.body)
if json_obj:
data = json.dumps(json_obj, indent=4)
2016-03-03 01:11:26 +00:00
data = "\njson = %s\n" % data
2016-03-01 13:08:55 +00:00
args += "\n json=json,"
else:
data = "\ndata = '''%s'''\n" % flow.request.body
args += "\n data=data,"
2016-02-08 14:37:38 +00:00
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
2016-03-03 01:11:26 +00:00
def is_json(headers, content):
2016-03-03 01:11:26 +00:00
if headers:
ct = parse_content_type(headers.get("content-type", ""))
if ct and "%s/%s" % (ct[0], ct[1]) == "application/json":
try:
return json.loads(content)
except ValueError:
return False
2016-03-03 01:11:26 +00:00
return False