Search code examples
pythonxmlntlm

Write Python NTLM XML response to local XML file


I have been looking all over to see if there is a simple way to write an NTLM response to a local XML file. How would I go about doing this?

import requests
import ntlm3 as ntlm
from requests_ntlm import HttpNtlmAuth

SITE = "website.com/_api/..."
USERNAME = 'user'
PASSWORD = 'pass'

response = requests.get(SITE, auth=HttpNtlmAuth(USERNAME,PASSWORD))

print(response.status_code)
print(response.text)

Solution

  • Assuming the NTLM response.text is a well-formed XML, simply dump the text value to file:

    xmlfile = open('Output.xml', 'wb')
    xmlfile.write(response_text)
    xmlfile.close()
    

    And to pretty print the output to file, consider lxml module:

    import lxml.etree as ET
    
    ...
    
    dom = ET.fromstring(response.text)
    tree_out = ET.tostring(dom, pretty_print=True)
    
    xmlfile = open('Output.xml', 'wb')
    xmlfile.write(tree_out)
    xmlfile.close()