Mitmproxy script returns "OSError: [Errno 63] File name too long" when XML response is read

Mitmproxy script returns "OSError: [Errno 63] File name too long" when XML response is read

我有一个简单的“mitmproxy”脚本,它应该修改响应的 XML 正文。但似乎 XML 正文太大,因为我收到以下错误“OSError:[Errno 63] 文件名太长:'from mitmproxy import ctx from mitmproxy import http import xml.etree.ElementTree as ET def response(flow: http.HTTPFlow) -> None: if flow.request.pretty_url.endswith("/someurl"): tree = ET.parse(flow.response.get_text()) root = tree.getroot() ET.register_namespace('', 'http://schema.blls.com/') for name in root.iter('name'): name.text = "joesmith" # tree = ET.ElementTree(root) tree.write(flow.response.text, encoding="UTF-8", xml_declaration=True)

当我 运行 脚本时,我得到了错误:

% mitmdump -s test.py
172.20.10.4:57657: GET https://.../someurl
                << 200 OK 1.63k
Addon error: Traceback (most recent call last):
  File "test.py", line 7, in response
    tree = ET.parse(flow.response.get_text())
  File "/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/xml/etree/ElementTree.py", line 1229, in parse
    tree.parse(source, parser)
  File "/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/xml/etree/ElementTree.py", line 569, in parse
    source = open(source, "rb")
OSError: [Errno 63] File name too long: '<?xml version="1.0" encoding="UTF-8...

你能告诉我我做错了什么吗? 谢谢!

看起来 ET.parse 需要一个文件名,而不是文件的内容。