V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ikamu
V2EX  ›  Python

用 Python 的 mitmproxy 库做请求拦截、修改时遇到'NoneType' object has no attribute 'text'

  •  
  •   ikamu · 2020-09-25 10:37:13 +08:00 · 2225 次点击
    这是一个创建于 1301 天前的主题,其中的信息可能已经有所发展或是发生改变。

    期望:在本地程序发起的请求到达本地代理时做出修改,直接返回给目标程序,不再到达服务器。 mitmproxy==5.0.1,python==3.6.1, 程序结构:

    class MyAdd:
        def request(self, flow):
            # do something in request
            if "pan.baidu.com" in flow.request.pretty_url:            
                flow.kill()
                
            if "test_kkk.cn" = flow.request.host:
                with open('./init') as f_init:
                    flow.response.text = f_init.read()
                    flow.response.status_code = 200
                    
        def response(self, flow):
            if "baidu.com" not in flow.request.pretty_url:
                print(flow.response.text)
    

    报错

        flow.response.text = f_init.read()
    AttributeError: 'NoneType' object has no attribute 'text'
    

    而如果把 if "test_kkk.cn" = flow.request.host 放到 response 函数下则能正确修改,但不能满足“直接返回给目标程序,不再到达服务器”的要求。 该怎么实现呢

    6 条回复    2020-09-25 13:24:17 +08:00
    ljhaoboy
        1
    ljhaoboy  
       2020-09-25 11:24:14 +08:00
    xiaolinjia
        2
    xiaolinjia  
       2020-09-25 11:27:08 +08:00
    虽然没用过这个库,不过看报错就是因为 flow.response=None 。
    于是推测原因是,在 request 方法里,这库还没有给 flow.response 对象实例化。
    然后问题又来了,不到达服务器,又怎么有 response 响应呢?
    xiaolinjia
        3
    xiaolinjia  
       2020-09-25 11:31:48 +08:00
    @xiaolinjia 看了下楼上,自己在 request 方法里实例化个 flow.Response 对象应该是可行的。
    ikamu
        4
    ikamu  
    OP
       2020-09-25 12:44:45 +08:00
    @ljhaoboy 看了下这个库,有点老以至于很多类的位置不再与目录文件对应。

    @xiaolinjia 出现问题的原因明白了,得在某处实例个 flow.Response 对象,这个依旧有点不知该在哪添。
    ikamu
        5
    ikamu  
    OP
       2020-09-25 12:46:48 +08:00
    结局:顺着一楼的链接中找到了新版的,https 。。。github 。com/mitmproxy/mitmproxy/blob/v5.x/examples/addons/http-reply-from-proxy.py 。虽然和实例个 flow.Response 对象,然后用 flow.response.text 进行赋值的预期目标有区别,但还算是符合。
    ```
    def start():
    Addon = MyAdd()
    options = Options(listen_host='127.0.0.1', listen_port=8080)
    config = ProxyConfig(options)
    m = DumpMaster (options)
    m.server = ProxyServer(config)
    m.addons.add(Addon)
    m.run()
    ```
    调用方式,期待其他答案。
    ikamu
        6
    ikamu  
    OP
       2020-09-25 13:24:17 +08:00
    方法找到了:添加 flow.response = http.HTTPResponse.make()
    ```
    from mitmproxy import http

    flow.response = http.HTTPResponse.make()
    if "test_kkk.cn" = flow.request.host:
    with open('./init') as f_init:
    flow.response.text = f_init.read()
    flow.response.status_code = 200
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1468 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 17:15 · PVG 01:15 · LAX 10:15 · JFK 13:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.