【问题标题】:Logging all http request with mitmproxy inline script使用 mitmproxy 内联脚本记录所有 http 请求
【发布时间】:2017-07-12 04:41:25
【问题描述】:

我正在尝试使用 mitmproxy 记录每个 http 站点,但我的内联脚本给出了这个错误 TypeError: request() missing 1 required positional argument: 'flow' 这是一个预览我的代码。我确实正确设置了代理,并且 httplogs.txt 文件与内联脚本位于同一目录中,但我不明白这个函数有什么问题。

import sys

def request(context,flow):
    f = open('httplogs.txt', 'a+')
    f.write(flow.request.url + '\n')
    f.close()

【问题讨论】:

    标签: python mitmproxy


    【解决方案1】:

    假设您使用的是更新的(2017 年 1 月)版本

    tl;dr

    从方法签名中删除context


    7 个月前 mitmproxy 从 response 方法中删除了 context

    https://github.com/mitmproxy/mitmproxy/commit/c048ae1d5b652ad4778917e624ace217e1ecfd91

    所以更新的示例脚本在这里:

    https://github.com/mitmproxy/mitmproxy/blob/1.0.x/examples/simple/add_header.py

    def response(flow):
        flow.response.headers["newheader"] = "foo"
    

    【讨论】:

    • 听起来和我的经历相似:)
    【解决方案2】:

    谢谢,解决了!

    这是我更新的用于记录请求和响应标头的 sn-p:

    def response(flow):
        print("")
        print("="*50)
        #print("FOR: " + flow.request.url)
        print(flow.request.method + " " + flow.request.path + " " + flow.request.http_version)
    
        print("-"*50 + "request headers:")
        for k, v in flow.request.headers.items():
            print("%-20s: %s" % (k.upper(), v))
    
        print("-"*50 + "response headers:")
        for k, v in flow.response.headers.items():
            print("%-20s: %s" % (k.upper(), v))
            print("-"*50 + "request headers:")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-02
      • 2013-10-09
      • 2016-03-09
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      相关资源
      最近更新 更多