1

I have a python script that does a REST GET call and stores the xml response in a string "response". However when I try to print the root of the XML, it fails with the following error. If i just print response i.e "print response.read()", I get the response body correctly. What could be wrong here? Could you please help?

import urllib
import urllib2
import xml.etree.ElementTree as ET

url = "http://192.168.1.1/health"
headers = {"Content-Type":"application/xml"}

request = urllib2.Request(url)

for key in headers.items():
  request.add_header(key)

response = urllib2.urlopen(request)

#print response.read()

root = ET.fromstring(response)

#print root

Here is the error when executing the script

~]# python test4.py
Traceback (most recent call last):
  File "test4.py", line 24, in <module>
    root = ET.fromstring(response)
  File "/usr/lib64/python2.6/xml/etree/ElementTree.py", line 963, in XML
    parser.feed(text)
  File "/usr/lib64/python2.6/xml/etree/ElementTree.py", line 1245, in feed
    self._parser.Parse(data, 0)
TypeError: Parse() argument 1 must be string or read-only buffer, not instance

1 Answer 1

2

Change this

root = ET.fromstring(response)

to

root = ET.fromstring(response.read())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.