{"id":161360,"date":"2020-09-10T07:22:22","date_gmt":"2020-09-09T23:22:22","guid":{"rendered":"http:\/\/4563.org\/?p=161360"},"modified":"2020-09-10T07:22:22","modified_gmt":"2020-09-09T23:22:22","slug":"%e7%94%a8-python-%e7%9a%84-mitmproxy-%e5%ba%93%e5%81%9a%e8%af%b7%e6%b1%82%e6%8b%a6%e6%88%aa%e3%80%81%e4%bf%ae%e6%94%b9%e6%97%b6%e9%81%87%e5%88%b0nonetype-object-has-no-attribute-text","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=161360","title":{"rendered":"\u7528 Python \u7684 mitmproxy \u5e93\u505a\u8bf7\u6c42\u62e6\u622a\u3001\u4fee\u6539\u65f6\u9047\u5230&#8217;NoneType&#8217; object has no attribute &#8216;text&#8217;"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u7528 Python \u7684 mitmproxy \u5e93\u505a\u8bf7\u6c42\u62e6\u622a\u3001\u4fee\u6539\u65f6\u9047\u5230&#8217;NoneType&#8217; object has no attribute &#8216;text&#8217;               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ikamu <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u671f\u671b\uff1a\u5728\u672c\u5730\u7a0b\u5e8f\u53d1\u8d77\u7684\u8bf7\u6c42\u5230\u8fbe\u672c\u5730\u4ee3\u7406\u65f6\u505a\u51fa\u4fee\u6539\uff0c\u76f4\u63a5\u8fd4\u56de\u7ed9\u76ee\u6807\u7a0b\u5e8f\uff0c\u4e0d\u518d\u5230\u8fbe\u670d\u52a1\u5668\u3002 mitmproxy==5.0.1\uff0cpython==3.6.1\uff0c \u7a0b\u5e8f\u7ed3\u6784\uff1a<\/p>\n<pre><code>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) <\/code><\/pre>\n<p>\u62a5\u9519<\/p>\n<pre><code>    flow.response.text = f_init.read() AttributeError: 'NoneType' object has no attribute 'text' <\/code><\/pre>\n<p>\u800c\u5982\u679c\u628a if &#8220;test_kkk.cn&#8221; = flow.request.host \u653e\u5230 response \u51fd\u6570\u4e0b\u5219\u80fd\u6b63\u786e\u4fee\u6539\uff0c\u4f46\u4e0d\u80fd\u6ee1\u8db3\u201c\u76f4\u63a5\u8fd4\u56de\u7ed9\u76ee\u6807\u7a0b\u5e8f\uff0c\u4e0d\u518d\u5230\u8fbe\u670d\u52a1\u5668\u201d\u7684\u8981\u6c42\u3002 \u8be5\u600e\u4e48\u5b9e\u73b0\u5462<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3483840\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ljhaoboy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/github.com\/mitmproxy\/mitmproxy\/blob\/v0.9.2\/examples\/redirect_requests.py<br \/>\u5eb7\u5eb7\u8fd9\u4e2a\uff0c\u597d\u50cf\u662f\u7b26\u5408\u4f60\u7684\u8981\u6c42\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3483841\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiaolinjia <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u867d\u7136\u6ca1\u7528\u8fc7\u8fd9\u4e2a\u5e93\uff0c\u4e0d\u8fc7\u770b\u62a5\u9519\u5c31\u662f\u56e0\u4e3a flow.response=None \u3002<br \/>\u4e8e\u662f\u63a8\u6d4b\u539f\u56e0\u662f\uff0c\u5728 request \u65b9\u6cd5\u91cc\uff0c\u8fd9\u5e93\u8fd8\u6ca1\u6709\u7ed9 flow.response \u5bf9\u8c61\u5b9e\u4f8b\u5316\u3002<br \/>\u7136\u540e\u95ee\u9898\u53c8\u6765\u4e86\uff0c\u4e0d\u5230\u8fbe\u670d\u52a1\u5668\uff0c\u53c8\u600e\u4e48\u6709 response \u54cd\u5e94\u5462\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3483842\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiaolinjia <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xiaolinjia \u770b\u4e86\u4e0b\u4e0a\uff0c\u81ea\u5df1\u5728 request \u65b9\u6cd5\u91cc\u5b9e\u4f8b\u5316\u4e2a flow.Response \u5bf9\u8c61\u5e94\u8be5\u662f\u53ef\u884c\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3483843\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ikamu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ljhaoboy \u770b\u4e86\u4e0b\u8fd9\u4e2a\u5e93\uff0c\u6709\u70b9\u8001\u4ee5\u81f3\u4e8e\u5f88\u591a\u7c7b\u7684\u4f4d\u7f6e\u4e0d\u518d\u4e0e\u76ee\u5f55\u6587\u4ef6\u5bf9\u5e94\u3002<\/p>\n<p>@xiaolinjia \u51fa\u73b0\u95ee\u9898\u7684\u539f\u56e0\u660e\u767d\u4e86\uff0c\u5f97\u5728\u67d0\u5904\u5b9e\u4f8b\u4e2a flow.Response \u5bf9\u8c61\uff0c\u8fd9\u4e2a\u4f9d\u65e7\u6709\u70b9\u4e0d\u77e5\u8be5\u5728\u54ea\u6dfb\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3483844\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ikamu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7ed3\u5c40\uff1a\u987a\u7740\u4e00\u7684\u94fe\u63a5\u4e2d\u627e\u5230\u4e86\u65b0\u7248\u7684\uff0chttps \u3002\u3002\u3002github \u3002com\/mitmproxy\/mitmproxy\/blob\/v5.x\/examples\/addons\/http-reply-from-proxy.py \u3002\u867d\u7136\u548c\u5b9e\u4f8b\u4e2a flow.Response \u5bf9\u8c61\uff0c\u7136\u540e\u7528 flow.response.text \u8fdb\u884c\u8d4b\u503c\u7684\u9884\u671f\u76ee\u6807\u6709\u533a\u522b\uff0c\u4f46\u8fd8\u7b97\u662f\u7b26\u5408\u3002<br \/>&#8220;`<br \/>def start():<br \/> Addon = MyAdd() <br \/> options = Options(listen_host=&#8217;127.0.0.1&#8242;, listen_port=8080)<br \/> config = ProxyConfig(options)<br \/> m = DumpMaster (options)<br \/> m.server = ProxyServer(config)<br \/> m.addons.add(Addon)<br \/> m.run()<br \/>&#8220;`<br \/>\u8c03\u7528\u65b9\u5f0f\uff0c\u671f\u5f85\u5176\u4ed6\u7b54\u6848\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3483845\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ikamu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u65b9\u6cd5\u627e\u5230\u4e86\uff1a\u6dfb\u52a0 flow.response = http.HTTPResponse.make()<br \/>&#8220;`<br \/>from mitmproxy import http<\/p>\n<p>flow.response = http.HTTPResponse.make()<br \/>if &#8220;test_kkk.cn&#8221; = flow.request.host:<br \/> with open(&#8216;.\/init&#8217;) as f_init:<br \/> flow.response.text = f_init.read()<br \/> flow.response.status_code = 200<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u7528 Python \u7684 mitmpr&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/161360"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=161360"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/161360\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=161360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=161360"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=161360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}