Errors in Python Script

Forum rules

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo

Posted on
Fri Sep 21, 2012 6:38 pm
bob offline
User avatar
Posts: 500
Joined: Jun 14, 2006

Errors in Python Script

I have the following Python script run every 1 and 1/2 minutes, in a 24 hour period I get a dozen or so errors. I was wondering if you had any suggestions to the script for the way I have the scrip connect to the network address 10.0.1.2.

Code: Select all
import xml.etree.cElementTree as ET
from xml.etree.cElementTree import parse
import urllib2
import string
import re
import socket
import time

socket.setdefaulttimeout(40)

#set prevTemp to last temp
prevAlertTemp = indigo.variables[77213772]
indigo.variable.updateValue(794594968, prevAlertTemp.value)

url = "http://10.0.1.2/xmlfeed.rb"
tree = parse(urllib2.urlopen(url))

#get all the port elements
for port in tree.findall("ports/port"):

    if port.get('name') == 'Port 1':
        currentTemp = port.find("condition/currentReading")
        indigo.variable.updateValue("tAlertTemp_", currentTemp.text)
       
    elif port.get('name') == 'Port 2':

        currentTemp = port.find("condition/currentReading")
        indigo.variable.updateValue("tAlertTemp2_", currentTemp.text)

indigo.variable.updateValue(865541269, time.strftime( "%H:%M %a %d %b"))


This is the error:

Script Error tempAlert.py: <urlopen error timed out>
Script Error Exception Traceback (most recent call shown last):

tempAlert.py, line 16, at top level
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 124, in urlopen
return _opener.open(url, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 381, in open
response = self._open(req, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 399, in _open
'_open', req)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 360, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 1118, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/urllib2.py", line 1093, in do_open
raise URLError(err)
URLError: <urlopen error timed out>


Thanks for any suggestions,

bob

Posted on
Fri Sep 21, 2012 8:33 pm
matt (support) offline
Site Admin
User avatar
Posts: 21424
Joined: Jan 27, 2003
Location: Texas

Re: Errors in Python Script

The code looks fine. I think the error is likely caused by whatever hardware you have at address 10.0.1.2 or is a networking problem. What type of hardware is it? Can it not handle requests every 30 seconds perhaps?

If you just want to avoid having the errors logged, then you can bracket everything around a try: / catch: exception handler.

Image

Posted on
Fri Sep 21, 2012 9:14 pm
bob offline
User avatar
Posts: 500
Joined: Jun 14, 2006

Re: Errors in Python Script

Matt,

It is a TemperatureAlert USB Wifi Temperature Monitor which is connected to my network. The script is parsing the temperature from the xml file it writes. I'll try contacting them.

Thanks for your help.

bob

Posted on
Sun Sep 30, 2012 2:31 pm
bschollnick2 offline
Posts: 1355
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Errors in Python Script

bob wrote:
socket.setdefaulttimeout(40)

#set prevTemp to last temp
url = "http://10.0.1.2/xmlfeed.rb"
try:
tree = parse(urllib2.urlopen(url))
connected = True
except URLError:
tree = None
connected = False


From there you can either:

if tree <> None:
<rest of code>

or:

if tree <> None:
return None (Exit from the function)

or:

if connected == True:

etc...
etc...


------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest