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 ie "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
Change this
root = ET.fromstring(response)
to
root = ET.fromstring(response.read())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.