pythonxmlmitmproxy

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


I have a simple "mitmproxy" script which should modify the response's XML body. But it seems that the XML body too large because I got the following error "OSError: [Errno 63] File name too long: '<?xml version="1.0" encodin..."

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)

And when I run the script I got the error:

% 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...

Could you tell me what I'm doing wrong? Thanks!


Solution

  • It looks like ET.parse expects a filename, not the contents of the file.