using rest and parsing xml in python -


i have python script rest call , stores xml response in string "response". when try print root of xml, fails following error. if print response i.e "print response.read()", response body correctly. wrong here? please help?

import urllib import urllib2 import xml.etree.elementtree et  url = "http://192.168.1.1/health" headers = {"content-type":"application/xml"}  request = urllib2.request(url)  key in headers.items():   request.add_header(key)  response = urllib2.urlopen(request)  #print response.read()  root = et.fromstring(response)  #print root 

here error when executing 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 string or read-only buffer, not instance 

change this

root = et.fromstring(response) 

to

root = et.fromstring(response.read()) 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -