masonc
Posts: 2
Joined: Thu Dec 11, 2014 1:31 pm

Python question: urllib2.urlopen xml parsing crashes

Thu Dec 11, 2014 2:03 pm

I am capturing an XML dataset over http, parsing the XML and returning a value. I am doing it in an infinite loop so as to control a relay.

fp = urllib2.urlopen(URL1, timeout = 60 )
doc = etree.parse(fp)
fp.close()

After some time of working properly, it crashes, possibly due to an internet outage. I need to prevent that. Any suggestions hot to handle this gracefully and keep the loop going?

Traceback (most recent call last):
File "./inverter_control_1.py", line 25, in <module>
doc = etree.parse(fp)
File "lxml.etree.pyx", line 2953, in lxml.etree.parse (src/lxml/lxml.etree.c:56204)
File "parser.pxi", line 1555, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:82511)
File "parser.pxi", line 1585, in lxml.etree._parseFilelikeDocument (src/lxml/lxml.etree.c:82832)
File "parser.pxi", line 1468, in lxml.etree._parseDocFromFilelike (src/lxml/lxml.etree.c:81688)
File "parser.pxi", line 1024, in lxml.etree._BaseParser._parseDocFromFilelike (src/lxml/lxml.etree.c:78735)
File "parser.pxi", line 569, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:74472)
File "parser.pxi", line 646, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:75329)
File "lxml.etree.pyx", line 282, in lxml.etree._ExceptionContext._raise_if_stored (src/lxml/lxml.etree.c:7459)
File "parser.pxi", line 376, in lxml.etree._FileReaderContext.copyToBuffer (src/lxml/lxml.etree.c:72692)
File "/usr/lib/python2.7/socket.py", line 380, in read
data = self._sock.recv(left)
File "/usr/lib/python2.7/httplib.py", line 541, in read
return self._read_chunked(amt)
File "/usr/lib/python2.7/httplib.py", line 574, in _read_chunked
line = self.fp.readline(_MAXLINE + 1)
File "/usr/lib/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
socket.timeout: timed out

Return to “Python”