0

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)
j doe
  • 233
  • 6
  • 19

1 Answers1

0

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()
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • If the file is kind of jumbled should I use ElementTree? This does not actually create the file right I need to create a file named "Output.xml" – j doe May 17 '16 at 21:10
  • Do you mean [pretty print](http://stackoverflow.com/questions/749796/pretty-printing-xml-in-python)? Minidom and lxml modules can do so. – Parfait May 17 '16 at 21:14
  • Also when I create the XML file and run the code I get xmlfile.write(response.text) io.UnsupportedOperation: not writable – j doe May 17 '16 at 21:15
  • Whoops! See update with the write binary, `wb` argument. – Parfait May 17 '16 at 21:17
  • Thank you! Is there any way I can do this without having to create the file manually? – j doe May 17 '16 at 21:22
  • Not quite familiar with [NTLM library](https://code.google.com/archive/p/python-ntlm/). See if there is an output to file functionality, but it seems response just migrates to string object for users to manipulate further for end use needs. For pretty print, you would need another operation. – Parfait May 17 '16 at 21:26